Posted by: rusteddev | July 19, 2010

Expose WCF Service with BizTalk 2010 tutorial

Hello world !

Today my first tutorial on BizTalk. I’ll show you how to expose a WebService through BizTalk using the “BizTalk WCF Service Publishing wizard”.

The business case is this : I want to expose a WS that will allow a partner to send me customer invoices. These invoices processed by BizTalk might then be sent both to SAP for partner’s billing and to a datawarehouse for analysis.

  • Step 1 – Prepare the data contract : Create a BizTalk project containing only 1 schema. It wil be used for the customer invoice message (Invoice.xsd). Build the project. Don’t forget to sign the assembly so that it can be deployed to GAC

Create XML Schema for your WS request

  • Step 2 – Prepare your Biztalk application : Create an empty biztalk application using BizTalk management console. The wizard will use this application to deploy your WCF Receive Location. Name it BizWCFTutorialApp.
create a  new application using biztalk administration console

create a new application using biztalk administration console

Now add the schema assembly to your BizTalk application (Right CLick on BizTalk Application then Add, BizTalk Assemblies). This will be useful for the Xm lReceive Pipeline of your WCF ReceiveLocation.

Add the .dll to your application

  • Step 3 – Run the wizard : Run the BizTalk WCF Service Publishing wizard. Choose the Service Endpoint with WCF-BasicHttp transport type, enable metadata endpoint and create BizTalk ReceiveLocation in the biztalk application BizWCFTutorialApp you’ve just created.

Choose Service Endpoint with WCF-BasicHttp adapter

Then choose to publish the previously created schemas as WCF Service.

publish schemas as WCF Service

In the next screen, you should  customize the Web Service description with the following operations. Use your right click :

  •  
    • Rename WCF service description to “BizTalkWcfServiceTutorial
    • Rename Web Service to “InvoiceService
    • Delete default Web-Method “Operation1” because its a request / response method.
    • Add a new one way web method called “setInvoice
    • Select the schema type “InvoiceSchemas.Invoice” for your request message. You will need to open the generated dll in your bin/debug folder.

Your screen should be like this :

Define your WCF service

To finish the wizard give the WCF Service a tagetnamespace (I suggest http://MyCompany.Services.InvoiceService) and choose a location (I leave default http://localhost/BizTalkWcfServiceTutorial). Allow anonymous access : it would be much easier for a first try.

Choose location and allow anonymous access

then you should be able to create the Service and end up to the following screen.

the wizard is over

  • Step 4 : Adapt IIS & BizTalk – In order for your WS to work you might need to change a few things.
    • First, as a good practice create a dedicated App Pool for your service. Go to IIS mangement console and create a new App Pool “BizTalkWcfServiceTutorialAppPool”. If you are like me and use BizTalk Server 2010, you must change the app pool .Net Framework to version  4.0. Then change your Service App Pool to the newly created one.

create a dedicated app pool

  •  
    • Give enough privileges to your app pool by choosing the right identity. I suggest admin for development purposes.
    • In BizTalk now :
      • Change the name of the wizard created WCF Receivelocation called WcfService_BizTalkWcfServiceTutorial/InvoiceService to something easier like “RcvLoc_Invoice_WCF”.
      • Change the ReceivePort to “RcvPort_Invoice_WCF”
      • Add a send port SendPort_Invoice_WCF and make it subscribe to messages from the app ReceivePort.
      • Start the application.

If you go to your WS page (normally http://localhost/BizTalkWcfServiceTutorial/InvoiceService.svc) and everything went fine you should be able to see this page :

your WCF Service should be up and ready

It migth fails because (I’ve been through this):

  •  
    • When choosing message type in the WCF Publishing wizard, you select a dll and a screen shows you availables schemas types available in the assembly so that you can choose wich one to expose as a service. In fact this is not really that. If you have the same .dll deployed to gac this screen shows you the gac dll content and not the content of the dll you’ve just selected. If like me you change and change again your Message types, you should be aware of that.

beware it might be Gac dll content not your dll content

 

  • Step 5 : Create the client application : Add a new console application project (named BizWCFTutorialClientApp) inside your already existing solution app (the one your crated for the XML Schema).
    • Add a new reference to System.ServiceModel inside your project

Add Reference to System.ServiceModel

  •  
    •  Add Service Reference to your previously created and deployed WS :

Add Service Reference

  •  
    • Your solution should look like below. Note that the app.config has been automatically generated.

Client App Project files

  •  
    • Now it’s time to hand code the call to your Service :
using System;
using BizWCFTutorialClientApp.InvoiceServiceReference;

namespace BizWCFTutorialClientApp
{
    class Program
    {
        static void Main(string[] args)
        {
            //Use the client proxy
            InvoiceServiceClient client = new InvoiceServiceClient();

            //Prepare the incoive
            Invoice i = new Invoice();
            i.Id = "1";
           
            //Add a line
            InvoiceLine l = new InvoiceLine();
            l.ProductEAN = "123123";
            l.Qty = "2";
            l.TaxAmount = 0.25;
            l.UnitPrice = 1000;
            i.Line = new InvoiceLine[1];
            i.Line[0] = l;

            try
            {
                //Create the Invoice via WS
                client.setInvoice(i);
            }
            catch (Exception e)
            {
                Console.Out.WriteLine("Error occured while calling the method setInvoice()");
                throw e;
            }
            finally
            {
                // Always close the client.
                client.Close();
            }

            Console.Out.WriteLine("Invoice created");

        }
    }
}

  • Step 6 : Test your Service and see the result in BizTalk

Now you are ready to Test your WS using the client and see the results in BizTalk. F5 the project “BizWCFTutorialClientApp”. You should see the following in BizTalk :

Tracked services instances

You can see 2 lines highlighted : the older represents the XML ReceivePipeline pipeline (notice it is run under BizTalk Isolated Host), it worked OK ; and the above line represents the send pipeline : pub/sub worked and message has been succesfully sent. See content matches your client code.

The result : invoice has been processed by BizTalk !

Done !

VS code (schemas, client console app, and bizTalk binding) is available here. As uploads of .zip files aren’t supported, you’ll have to delete the .jpg after saving the file to your computer.  Sorry about that.

I hope this tutorial is usefull to you : let me know !


Responses

  1. Hi Rusteddev,
    This entry is very usefull for me, thanks about this.
    I have a question that have you ever used biztalk for intergrating 2 wcf application ? Could you post another tutor like this.

    Many thanks

    • Hi Phuoc,

      Thank you for your feedback. I’m glad it helped you, that’s one of the reason why I made this blog. I did not practice wcf to wcf communication myself yet. I’m putting appart my C# and Biztalk skills for a while…. I’m practicing Ruby on Rails and XP programming for the time beeing. Maybe I’ll get back to BizTalk dev one day, who knows….

      • Hello,
        Could you share VC Code, I can’t downolad from your blog. Mayby you can send via email to me.

  2. Hi,
    I would like to appriciate you.This is a very good artical thanks and keep on posting more like this.

    Thanks,
    Purna

  3. hi
    this is very nice tutorial that helps more for biztalk learner.
    thanks for sending and keep on posting like this

  4. This is very nice tutorial. I have scenario where I have to execute stored procedure for getting the data but without using pollingDataAvailableStatement and PollingDataStatement for say GetInvoice ? Any suggestion

  5. Hi , great post, it really helps me, I´m newbie in biztalk.
    This is my first time publishing schemas and this is the best tutorial i have found. THANK YOU VERY MUCH!
    But i have a problem, calling client.SetInvoice(i), i get this error

    “The server was unable to process the request due to an internal error. For more information about the error, either turn on IncludeExceptionDetailInFaults (either from ServiceBehaviorAttribute or from the configuration behavior) on the server in order to send the exception information back to the client, or turn on tracing as per the Microsoft .NET Framework 3.0 SDK documentation and inspect the server trace logs.”

    If i type in the Client app.config, i get
    {“Unrecognized configuration section serviceDebug. }
    in InvoiceServiceClient client = new InvoiceServiceClient();

    Any idea??
    Thanks
    MEL

  6. Very Nice Article to understand the concept of Publish BizTalk Schema as WCF Service.

  7. Thanks a ton. It was very helpful for someone new to BizTalk WCF Service publishing wizard..And thanks for providng client information on invoking the service as well..great job!!

  8. Great Tutorial. Thanks.


Leave a reply to Purnachandra Cancel reply

Categories