Chaks' Corner

SharePoint and other stuffs

22. October 2008 04:14
by Chakkaradeep
0 Comments

Consuming WCF Service in SharePoint

22. October 2008 04:14 by Chakkaradeep | 0 Comments

Today, I had to consume a WCF Service in my SharePoint Feature. I was sure of not using svcutil or Visual Studio 2008's Add Service Reference because that adds extra files that I might need to consider deploying along with my SharePoint solution. So, how do we consume our service ?

Below are the basic steps if you want to create your WCF Client manually via the code:

Put the necessary web.config entries into your SharePoint's application's web.config . A typical example would be:

   1: <system.serviceModel>
   2:     <bindings>
   3:       <wsHttpBinding>
   4:         <binding name="insert-your-binding-configuration">
   5:           <security mode="Message">
   6:             <transport clientCredentialType="Windows" 
   7:                        proxyCredentialType="None" realm="" />
   8:             <message clientCredentialType="Windows" 
   9:                      negotiateServiceCredential="true" 
  10:                      algorithmSuite="Default" 
  11:                      establishSecurityContext="true" />
  12:           </security>
  13:         </binding>
  14:       </wsHttpBinding>
  15:     </bindings>
  16:     <client>
  17:       <endpoint 
  18:         address="insert-your-address" 
  19:         binding="wsHttpBinding" 
  20:         bindingConfiguration="insert-your-binding-configuration" 
  21:         contract="insert-your-contract-description"
  22:         name="insert-your-endpoint-name">        
  23:       </endpoint>
  24:    </client>
  25: </system.serviceModel>
Create a Service Client Channel from your ServiceContract
public interface IMyServiceChannel : IMyService, IClientChannel {}

The IMyService is my service contract

Create a Channel Factory and associate with the endpoint configuration name

ChannelFactory<IMyServiceChannel> channelFactory = 
    new ChannelFactory<IMyServiceChannel>("insert-your-endpoint-name")

With the factory, now we can create our Proxy

IMyServiceChannel proxy = channelFactory.CreateChannel()

You are now ready! Wrap these in a using statement and invoke your service methods using the proxy created above :)

Comments are closed