views:

342

answers:

2

I have the following app.config section that I need to translate into code. I have seen several examples, but still cannot quite get it to work. Could anyone help?

<system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="MyService" 
                 closeTimeout="00:01:00"
                 openTimeout="00:01:00" 
                 receiveTimeout="00:10:00" 
                 sendTimeout="00:01:00"
                 allowCookies="false" 
                 bypassProxyOnLocal="false" 
                 hostNameComparisonMode="StrongWildcard"
                 maxBufferSize="65536" 
                 maxBufferPoolSize="524288" 
                 maxReceivedMessageSize="65536"
                 messageEncoding="Text" 
                 textEncoding="utf-8" 
                 transferMode="Buffered"
                 useDefaultWebProxy="true">
            <readerQuotas maxDepth="32" 
                          maxStringContentLength="8192" 
                          maxArrayLength="16384"
                          maxBytesPerRead="4096" 
                          maxNameTableCharCount="16384" />
            <security mode="TransportWithMessageCredential">
                <transport clientCredentialType="None" 
                           proxyCredentialType="None" 
                           realm="" />
                <message clientCredentialType="UserName" 
                         algorithmSuite="Default" />
            </security>
        </binding>
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint address="https://server.com/service/MyService.asmx"
      binding="basicHttpBinding" bindingConfiguration="MyService"
      contract="MyService.MyServiceInterface"
      name="MyService" />
    </client>
</system.serviceModel>

My use case is that I am writing a dll that will be used by other non-.net applications, and henceforth I have no good place to put the app.config.

Thanks!

+2  A: 

You could use something like this (it looks like pretty standard basicHttpBinding):

BasicHttpBinding binding = new BasicHttpBinding();
Uri endpointAddress = new Uri("https://server.com/service/MyService.asmx");

ChannelFactory<MyService.MyServiceInterface> factory = new ChannelFactory<MyService.MyServiceInterface>(binding, endpointAddress);

MyService.MyServiceInterface proxy = factory.CreateChannel();

This works as long as you have a DLL which contains the contract ("MyService.MyServiceInterface") available and you can reference it in your client.

If you need this on the service side, you'll have to use some different classes etc - but the basics are the same (create a binding, create one or more endpoint addresses, bind them).

Marc

PS: Sorry, I just noticed you use a https:// address - that might require some additional security configuration in code.

marc_s
A: 

Thank you marc_s, you led me in the right direction!

For anyone interested, here is the code to make it work with SSL as well:

        BasicHttpBinding binding = new BasicHttpBinding();
        binding.Security.Mode = BasicHttpSecurityMode.TransportWithMessageCredential;
        Uri endpointAddress = new Uri("https://server.com/Service.asmx");

        ChannelFactory<MyService.MyServiceInterface> factory = new ChannelFactory<MyService.MyServiceInterface>(binding, endpointAddress.ToString());
        factory.Credentials.UserName.UserName = "username";
        factory.Credentials.UserName.Password = "password";

        MyService.MyServiceInterface client = factory.CreateChannel();

        // make use of client to call web service here...
Eyvind
OK, glad I was able to help! (I'm learning for the WCF exam right now so this stuff ought to be fresh on my mind :-)
marc_s