在运行应用程序可执行文件

时间:2017-12-07 05:26:31

标签: c# .net winforms wcf visual-studio-2015

在过去的几周里,我开发了一个需要与32位DLL通信的64位WinForms应用程序(作业规范需要它)。

在互联网上做了一些阅读并发现没有任何有趣的方法可以做到这一点后,我决定在我的WinForms应用程序中托管WCF服务应用程序,以便与32位DLL进行通信。 ..或者我认为我在做。

在开发过程中(在Visual Studio中运行)它一直运行得很好,但当然,现在我需要部署,我遇到了问题。我无法对WCF服务有足够的了解,知道我是否会以一种糟糕的方式解决这个问题,或者我是否只是错过了一些细节。

我创建了项目为Admin。在开发完成后,我尝试运行WinForm可执行文件(调试和发布),WindowsFormsApplication1.exe。应用程序启动了,但在我尝试完成涉及使用WCF服务的任务后,抛出了一个异常:

exception messages

这让我相信Visual Studio在开发期间主持服务而不是WinForm应用程序,或者我的配置和/或目录结构不正确。

[更新] WCF服务Web.config:

<?xml version="1.0"?>
<configuration>

  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5.2" />
    <httpRuntime targetFramework="4.5.2" maxRequestLength="2147483647"/>
  </system.web>

  <system.net>
    <defaultProxy>
      <proxy usesystemdefault="False"/>
    </defaultProxy>
  </system.net>

  <system.diagnostics>
    <sources>
      <source name="System.ServiceModel"
              switchValue="Information, ActivityTracing"
              propagateActivity="true" >
        <listeners>
          <add name="xml"/>
        </listeners>
      </source>
      <source name="System.ServiceModel.MessageLogging">
        <listeners>
          <add name="xml"/>
        </listeners>
      </source>
      <source name="myUserTraceSource"
              switchValue="Information, ActivityTracing">
        <listeners>
          <add name="xml"/>
        </listeners>
      </source>
    </sources>
    <sharedListeners>
      <add name="xml"
           type="System.Diagnostics.XmlWriterTraceListener"
                 initializeData="C:\logs\Traces.svclog" />
    </sharedListeners>
  </system.diagnostics>

  <system.serviceModel>
    <diagnostics wmiProviderEnabled="true">
      <messageLogging
           logEntireMessage="true"
           logMalformedMessages="true"
           logMessagesAtServiceLevel="true"
           logMessagesAtTransportLevel="true"
           maxMessagesToLog="3000"
       />
    </diagnostics>
    <behaviors>
      <serviceBehaviors>
        <behavior name="metadadiscovery>
          <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="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>

<services>
  <service name="ServiceReference1.Service1" behaviorConfiguration="metadadiscovery">
    <endpoint address="" binding="basicHttpBinding" contract="ServiceReference1.IService1"></endpoint>
  </service>
</services>

    <protocolMapping>
        <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>    
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <!--
        To browse web app root directory during debugging, set the value below to true.
        Set to false before deployment to avoid disclosing web app folder information.
      -->
    <directoryBrowse enabled="false"/>
  </system.webServer>

</configuration>

WinForm App.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
  </startup>
  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpBinding_IService2" />
        <binding name="BasicHttpBinding_IService3" />
        <binding name="BasicHttpBinding_IService1" />
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint address="http://localhost:45053/Service2.svc" binding="basicHttpBinding"
        bindingConfiguration="BasicHttpBinding_IService2" contract="ServiceReference2.IService2"
        name="BasicHttpBinding_IService2" />
      <endpoint address="http://localhost:46351/Service3.svc" binding="basicHttpBinding"
        bindingConfiguration="BasicHttpBinding_IService3" contract="ServiceReference3.IService3"
        name="BasicHttpBinding_IService3" />
      <endpoint address="http://localhost:44848/Service1.svc" binding="basicHttpBinding"
        bindingConfiguration="BasicHttpBinding_IService1" contract="ServiceReference1.IService1"
        name="BasicHttpBinding_IService1" />
    </client>
  </system.serviceModel>
  <appSettings>
    <add key="ClientSettingsProvider.ServiceUri" value="" />
  </appSettings>
  <system.web>
    <membership defaultProvider="ClientAuthenticationMembershipProvider">
      <providers>
        <add name="ClientAuthenticationMembershipProvider" type="System.Web.ClientServices.Providers.ClientFormsAuthenticationMembershipProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" />
      </providers>
    </membership>
    <roleManager defaultProvider="ClientRoleProvider" enabled="true">
      <providers>
        <add name="ClientRoleProvider" type="System.Web.ClientServices.Providers.ClientRoleProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" cacheTimeout="86400" />
      </providers>
    </roleManager>
  </system.web>
</configuration>

EXE所在的目录:

directory

包含WCF服务的目录位于上图中的目录WcfService1中。

我大多使用以下实例化服务的方法:

ServiceReference1.Service1Client = new ServiceReference1.SErvice1Client();

一旦我尝试切换到使用服务主机(下面),但是当我使用该方法时,只要它尝试与DLL通信,服务就会超时。

Uri address = new Uri("http://localhost:44848/Service1.svc");
ServiceHost host = new ServiceHost(typeof(ServiceReference1.Service1Client), address);
host.Open();

然后我稍后关闭了主机。在这一点上,我愿意尝试任何事情来实现这一目标。

[编辑]下面是我的WindowsFormsApplication1.exe.config文件的代码。所有三个合同都根据其数据类型'clientContractType'“发出警告称它们”无效“。我认为这可能是我的问题的根源,但我不知道它为什么会出现这个警告:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
  </startup>
  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpBinding_IService2" />
        <binding name="BasicHttpBinding_IService3" />
        <binding name="BasicHttpBinding_IService1" />
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint address="http://localhost:45053/Service2.svc" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IService2" contract="ServiceReference2.IService2" name="BasicHttpBinding_IService2" />
      <endpoint address="http://localhost:46351/Service3.svc" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IService3" contract="ServiceReference3.IService3" name="BasicHttpBinding_IService3" />
      <endpoint address="http://localhost:44848/Service1.svc" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IService1" contract="ServiceReference1.IService1" name="BasicHttpBinding_IService1" />
    </client>
  </system.serviceModel>
</configuration>

感谢您提供的任何帮助和指导。

2 个答案:

答案 0 :(得分:0)

没有为您的服务配置端点。

    <behaviors>
          <serviceBehaviors>
            <behavior name="metadadiscovery">
              <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
              <serviceMetadata httpGetEnabled="true" httpsGetEnabled="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>
     <service name="ServiceReference2.Yourimplementingservice" behaviorConfiguration="metadadiscovery">
      <endpoint address="" binding="basicHttpBinding" contract="ServiceReference2.IService2">          
       </endpoint>

上面我已经为Service2配置了,类似地你必须配置Service1和Service3。

答案 1 :(得分:0)

经过充分的努力,我决定摆脱Visual Studio为您创建的WCF服务,当您将其创建为新项目时​​。我改为遵循本教程:

Hosting Service In App

这样做有很大的好处:

  1. 无需配置文件
  2. 以这种方式运行必须已经摆脱了大量的开销,因为与DLL的通信(我正在使用的服务)过去花费几秒钟来对DLL进行大量调用,但现在能够在眨眼之间处理10k +以上的电话。
  3. 无需服务参考。我只需要我的main函数文件,包含服务函数实现的文件和包含实现接口的文件。
  4. 到目前为止,这是我在64位应用程序中使用32位DLL时发现的最简单,最强大的方法。如果我能为任何可能正在努力解决这个问题的人提供任何指导,请告诉我。我知道,如果你之前从未做过类似的事情,这不是一件有趣的事情。