简体   繁体   English

WCF 服务在 VS 中有效,但在构建中无效

[英]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.我有一个 wcf 自托管服务,旨在供计算机和 android 手机使用。 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.我成功地使服务在两个目标中都可以使用,但是当我构建一个 exe 文件(负责自托管服务)时,该服务在 pc 中运行良好,但在移动应用程序上却无法运行。 here is my service's app.config file:这是我的服务的 app.config 文件:

<?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).我们必须考虑的一件事是,在托管程序(例如 WinForms 应用程序)中托管服务时,我们不需要配置服务端点和绑定信息。 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).当我们在Visual Studio上运行托管服务时,系统会自动搜索服务配置,这样Appconfig文件中的配置就会生效,正确绑定会生成不同种类的服务(restful和soap web服务)。 But the service by using NetHttpBinding will be different from the aforementioned service when hosting the service in other programs.但是使用 NetHttpBinding 的服务在其他程序中托管服务时会与上述服务有所不同。
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.它将自动在 Appconfig 文件中搜索服务配置,以便以适当的方式发布服务。
Feel free to let me know if the problem still exists.如果问题仍然存在,请随时告诉我。

You have configured your address to be localhost .您已将地址配置为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.确保您阅读了相关教程并选择您需要的地址或 DNS 条目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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