Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to config clients for a wcf service?

Tags:

wcf

I am developing a wcf service . I have created two dll's one for message contracts and one for service contracts interfaces. I share these two dll's with server and client. I am not using AddServiceReference i am using ChannelFactory class to create proxies. Following is the code which i am using to create client proxies:

BasicHttpBinding binding = new BasicHttpBinding(); 
EndpointAddress endpoint = new EndpointAddress(new Uri   ("http://localhost:8989/HelloService/"));
ChannelFactory<IHello> chanFac = new ChannelFactory<IHello>(binding, endpoint);
IHello clientProxy = chanFac.CreateChannel();

Now I have to create the binding and EndpointAddress in the code,what i want that this should come from app.config file , how can i do it so that i don't need to write binding and endpoint everytime in the code.... Any help is appreciated..

like image 984
Embedd_0913 Avatar asked Aug 23 '09 07:08

Embedd_0913


1 Answers

Use an app.config like this (when you use "Add Service Reference" from Visual Studio, VS will typically create this for you automatically - and you just need to tweak it to your needs):

<configuration>
    <system.serviceModel>
        <bindings>
          <basicHttpBinding>
            <binding name="UserNameSecurity">
              <security mode="Message">
                <message clientCredentialType="UserName"/>
              </security>
            </binding>
          </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://localhost:8888/MyService" binding="basicHttpBinding"
                bindingConfiguration="UserNameSecurity" contract="IMyService" />
            <endpoint address="net.tcp://localhost:8484/MyService/Mex" 
                      binding="mexTcpBinding"
                      bindingConfiguration="" 
                      contract="IMetadataExchange" name="mexNetTcp" />
        </client>
    </system.serviceModel>
</configuration>

The section and its possible values and subsection are well documented in the WCF configuration.

Alternatively, in VS 2008 SP1, you can use the "WCF Service Configuration Editor" - see it in "Tools > WCF Service Configuration Editor".

alt text

It allows you to visually define and modify your client config settings. Once you've launched it from the Tools menu, after that, you can acutally even right-click on the app.config in your Solution Explorer and launch it from there (using that app.config as its basis).

alt text

Marc

like image 175
marc_s Avatar answered Sep 20 '22 00:09

marc_s