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..
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".
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).
Marc
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With