简体   繁体   中英

WCF service works in VS but not in build

I have a wcf self hosted service that is meant to be consumed by both computers and android phones. i successfully made the service consumable in both targets but when i built an exe file (which is responsible for self hosting the service) the service worked great in the pc but it did not work on the mobile app. here is my service's app.config file:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" />
  </system.web>
  <!-- When deploying the service library project, the content of the config file must be added to the host's 
  app.config file. System.Configuration does not support config files for libraries. -->
  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="httpBinding" maxBufferSize="128000000" maxReceivedMessageSize="128000000" />
      </basicHttpBinding>
      <webHttpBinding>
        <binding name="webBinding" closeTimeout="00:02:00" sendTimeout="00:02:00"
          maxBufferSize="6553600" maxReceivedMessageSize="6553600" crossDomainScriptAccessEnabled="true" />
      </webHttpBinding>
    </bindings>
    <diagnostics performanceCounters="Default" />
    <services>
      <service name="DeltaService.Data">
        <endpoint address="data" binding="basicHttpBinding" bindingConfiguration="httpBinding"
          name="data" contract="DeltaService.IData">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="json" behaviorConfiguration="Rest" binding="webHttpBinding"
          bindingConfiguration="webBinding" name="restdata" contract="DeltaService.IData">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8000/delta_api/" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <endpointBehaviors>
        <behavior name="Rest">
          <webHttp helpEnabled="true" defaultOutgoingResponseFormat="Json" />
        </behavior>
        <behavior name="Web">
          <webHttp />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="">
          <useRequestHeadersForMetadataAddress />
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment
      aspNetCompatibilityEnabled="true"
      multipleSiteBindingsEnabled="true"/>


  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="mssqllocaldb" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>

</configuration>

and here is the code that launches the service:

serviceHost = new ServiceHost(typeof(Data));
NetHttpBinding netHttpBinding = new NetHttpBinding();
netHttpBinding.MaxReceivedMessageSize = 128 * 1000000; //128Mb
serviceHost.AddServiceEndpoint(typeof(IData), netHttpBinding, url);
erviceHost.Open();

It seems that you host the service by using other hosting programs. One thing we must take into consideration is that we needn't configure the service endpoint and binding information when hosting the service in a hosting program(such as WinForms application). There is a distinction when hosting in those two ways. When we hosting the service by running on the Visual studio, the system will automatically search for the service configuration, thus the configuration in the Appconfig file will take effect, proper binding generates different kinds of service (restful and soap web service). But the service by using NetHttpBinding will be different from the aforementioned service when hosting the service in other programs.
In a word, I suggest you use below code in the self-hosting program.

ServiceHost sh = new ServiceHost(typeof(Data));
            sh.Open();

It will automatically search for the service configuration in Appconfig file so that the service will be published in proper ways.
Feel free to let me know if the problem still exists.

You have configured your address to be localhost . That address can only be reached from your computer, not from the outside.

When you host your service, pick an address that can be reached from the outside. As I don't know where your device is, I don't know how much "outside" you need. Local network? WAN? Make sure you read a tutorial on that and pick the address or DNS entry you need.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM