views:

37

answers:

1

I am hosting a WCF Service as a Windows Service.

A snapshot below.

myHost = new ServiceHost(typeof(AnalyticsService));
Uri address = new Uri("http://localhost:8080/MathServiceLibrary");

WSHttpBinding binding = new WSHttpBinding();
binding.MaxReceivedMessageSize = 2147483647;
binding.MaxBufferPoolSize = 2147483647;

//binding.Security.Mode = SecurityMode.None;
Type contract = typeof(IAnalyticsService);

myHost.AddServiceEndpoint(contract,binding,address);

As you can see I have previously been only exposing the Service locally. I would like add another ServiceEndpoint so that other machines on my network can also call the service.

I assume I would need to add something like this to the above code:

myHost = new ServiceHost(typeof(AnalyticsService));

Uri address = new Uri("http://localhost:8080/MathServiceLibrary");
Uri new address = new Uri("http://xxx.xxx.xxx:8080/MathServiceLibrary");

WSHttpBinding binding = new WSHttpBinding();
binding.MaxReceivedMessageSize = 2147483647;
binding.MaxBufferPoolSize = 2147483647;

Type contract = typeof(IAnalyticsService);

myHost.AddServiceEndpoint(contract, binding, address);
myHost.AddServiceEndpoint(contract, binding, newaddress);

Now my current service library APP config looks like:

 <system.serviceModel>
    <services>
      <service behaviorConfiguration="ServiceLibrary.Service1Behavior"
        name="ServiceLibrary.AnalyticsService">
        <endpoint address="" binding="wsHttpBinding" contract="ServiceLibrary.IAnalyticsService">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8731/Design_Time_Addresses/ServiceLibrary/Service1/" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceLibrary.Service1Behavior">
          <!-- To avoid disclosing metadata information, 
          set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="True"/>
          <!-- To receive exception details in faults for debugging purposes, 
          set the value below to true.  Set to false before deployment 
          to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="False" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

While my app.config in the Service Library host is

  <system.serviceModel>
    <services>
      <service name="ServiceLibrary.AnalyticsService"
               behaviorConfiguration ="MathServiceMEXBehavior">
        <endpoint address="mex"
                  binding="mexHttpBinding"
                  contract="IMetadataExchange"/>
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8080/MathService"/&gt;
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="MathServiceMEXBehavior">
          <serviceMetadata httpGetEnabled="true"/>
          <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

I assume I need to add another base address to the service library file with the external (non-local-host) address. I am confused about what to alter in the Service Library File.

A: 

No, you cannot add a second http based base address - you only get to choose one per protocol - one for http, one for net.tcp and so on.

What you need to do is create a second endpoint in your app.config for your service host application (the app.config from the service library will never even be used - forget about that file), and assign it a fully qualified address.

<system.serviceModel>
    <services>
      <service name="ServiceLibrary.AnalyticsService"
               behaviorConfiguration ="MathServiceMEXBehavior">

        <endpoint name="firstEndpoint"
                  address=""
                  binding="wsHttpBinding"
                  contract="IAnalyticsService" />

        <endpoint name="secondEndpoint"
                  address="http://xxx.xxx.xxx:8080/MathServiceLibrary"
                  binding="wsHttpBinding"
                  contract="IAnalyticsService" />

        <endpoint address="mex"
                  binding="mexHttpBinding"
                  contract="IMetadataExchange"/>
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8080/MathService"/&gt;
          </baseAddresses>
        </host>
      </service>
    </services>
  </system.serviceModel>

So the firstEndpoint would use the base address and you'd be able to call it under

http://localhost:8080/MathService

while the secondEndpoint uses a specific, fully qualified URL and you'd be able to call it under

http://xxx.xxx.xxx:8080/MathServiceLibrary

However: why do you want to have a second endpoint at all?? You normally only have a second endpoint if you want to expose different protocols, e.g. you'd typically have one http, one https, one net.tcp endpoint and so on. Having two separate http endpoints with the same contract and same binding doesn't really make a lot of sense to me.....

marc_s
I should have been more clear. All I REALLy want to do is change the localhost to the IP and have the service available to other machines as well as be able to call the service from a client on the local machine.
bearrito