托管应用程序中的自托管 WCF Web 服务更改服务的定义

Self host WCF Web Service in Managed Application change the definition of the Service

我正在努力找出问题所在,但我做不到,而且我在 google 上找不到合适的解决方案。

我在 Visual Studio 的 Class 库项目中有一个 WCF Web 服务,具有接口 IWebService 、实现 WebService 和配置文件 App.config 暴露了两个方法 Instruction(string)Update(),服务获取和指令,将其保存在列表 FIFO 中,return 一个响应,然后我可以检索第一个元素用第二种方法列出。

IWebService.cs

 [ServiceContract(Namespace = "SMS_Application")]
  [XmlSerializerFormat]
  public interface IWebService
  {
    [OperationContract(Action = "Instruction", Name = "Instruction", ReplyAction = "InstructionResponse")]
    string[] Instruction(string Username, string Password, string Command);

    [OperationContract(Action = "GetUpdate", Name = "GetUpdate")]
    string GetUpdate(string Username, string Password);
}

App.config

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

  <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>
    <services>
      <service name="Callback">
        <endpoint address="" binding="basicHttpBinding" name="Callback"
          contract="SLWebServiceServer.IWebService">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8733/SLWebServiceServer/" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          ...
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

</configuration>

如果我在 visual studio 中的同一个解决方案(所以两个项目,一个解决方案)中创建一个带有自主机(使用 ServiceHost)的控制台应用程序,然后启动控制台应用程序,一切正常预期的。 问题是我有一个大应用程序,在多个解决方案中有多个 DLL,我想要的是在一个 DLL 中添加对 WebService 的引用并启动自托管。但是,如果我添加对 WebService 的引用并启动服务、定义 cahnge 以及 URL,但如果我将 WebService 项目添加到当前解决方案,所有 return 将按预期工作。

我找不到问题,我认为这与 App.config 文件有关,但我找不到什么。

这就是我创建自我主机的方式:

Uri baseAddress = new Uri(URL);
         string URL = "http://localhost:8733/SLWebServiceServer/";
        // Create a ServiceHost instance.
        ServiceHost _host = new ServiceHost(typeof(WebService), baseAddress);
        try {
          // Add a service endpoint.
          var binding = new WSHttpBinding();
          _host.AddServiceEndpoint(typeof(IWebService), binding, "Application");
          var behavior = _host.Description.Behaviors.Find<ServiceBehaviorAttribute>();
          behavior.InstanceContextMode = InstanceContextMode.Single;
          // Enable metadata exchange.
          ServiceMetadataBehavior smb = new ServiceMetadataBehavior {
            HttpGetEnabled = true
          };
          _host.Description.Behaviors.Add(smb);
          // Start the service.
          _host.Open();
        }
        catch (CommunicationException ce) {
          _host.Abort();
        }

这是它使用具有两个项目的解决方案创建的 WSDL 文件

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsx="http://schemas.xmlsoap.org/ws/2004/09/mex" xmlns:i0="http://tempuri.org/" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsa10="http://www.w3.org/2005/08/addressing" xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsap="http://schemas.xmlsoap.org/ws/2004/08/addressing/policy" xmlns:msc="http://schemas.microsoft.com/ws/2005/12/wsdl/contract" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:tns="SMS_Application" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" name="WCallback" targetNamespace="SMS_Application">
<wsdl:import namespace="http://tempuri.org/" location="http://192.168.8.101:8733/SLWebServiceServer/?wsdl=wsdl0"/>
<wsdl:types>
<xsd:schema targetNamespace="SMS_Application/Imports">
<xsd:import schemaLocation="http://192.168.8.101:8733/SLWebServiceServer/?xsd=xsd0" namespace="SMS_Application"/>
</xsd:schema>
</wsdl:types>
<wsdl:message name="IWebService_Instruction_InputMessage">
<wsdl:part name="parameters" element="tns:Instruction"/>
</wsdl:message>
<wsdl:message name="IWebService_Instruction_OutputMessage">
<wsdl:part name="parameters" element="tns:InstructionResponse"/>
</wsdl:message>
<wsdl:message name="IWebService_GetUpdate_InputMessage">
<wsdl:part name="parameters" element="tns:GetUpdate"/>
</wsdl:message>
<wsdl:message name="IWebService_GetUpdate_OutputMessage">
<wsdl:part name="parameters" element="tns:GetUpdateResponse"/>
</wsdl:message>
<wsdl:portType name="IWebService">
<wsdl:operation name="Instruction">
<wsdl:input wsaw:Action="Instruction" message="tns:IWebService_Instruction_InputMessage"/>
<wsdl:output wsaw:Action="InstructionResponse" message="tns:IWebService_Instruction_OutputMessage"/>
</wsdl:operation>
<wsdl:operation name="GetUpdate">
<wsdl:input wsaw:Action="GetUpdate" message="tns:IWebService_GetUpdate_InputMessage"/>
<wsdl:output wsaw:Action="SMS_Application/IWebService/GetUpdateResponse" message="tns:IWebService_GetUpdate_OutputMessage"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:service name="Callback">
<wsdl:port name="Callback" binding="i0:Callback">
<soap:address location="http://192.168.8.101:8733/SLWebServiceServer/"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>

这只有一个项目,WebService 仅作为外部参考

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:tns="SMS_Application" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:wsx="http://schemas.xmlsoap.org/ws/2004/09/mex" xmlns:wsap="http://schemas.xmlsoap.org/ws/2004/08/addressing/policy" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" xmlns:msc="http://schemas.microsoft.com/ws/2005/12/wsdl/contract" xmlns:i0="http://tempuri.org/" xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsa10="http://www.w3.org/2005/08/addressing" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" name="Callback" targetNamespace="SMS_Application">
<wsdl:import namespace="http://tempuri.org/" location="http://192.168.8.101:8733/SLWebServiceServer/?wsdl=wsdl0"/>
<wsdl:types>
<xsd:schema targetNamespace="SMS_Application/Imports">
<xsd:import schemaLocation="http://192.168.8.101:8733/SLWebServiceServer/?xsd=xsd0" namespace="SMS_Application"/>
</xsd:schema>
</wsdl:types>
<wsdl:message name="IWebService_Instruction_InputMessage">
<wsdl:part name="parameters" element="tns:Instruction"/>
</wsdl:message>
<wsdl:message name="IWebService_Instruction_OutputMessage">
<wsdl:part name="parameters" element="tns:InstructionResponse"/>
</wsdl:message>
<wsdl:message name="IWebService_GetUpdate_InputMessage">
<wsdl:part name="parameters" element="tns:GetUpdate"/>
</wsdl:message>
<wsdl:message name="IWebService_GetUpdate_OutputMessage">
<wsdl:part name="parameters" element="tns:GetUpdateResponse"/>
</wsdl:message>
<wsdl:portType name="IWebService">
<wsdl:operation name="Instruction">
<wsdl:input wsaw:Action="Instruction" message="tns:IWebService_Instruction_InputMessage"/>
<wsdl:output wsaw:Action="InstructionResponse" message="tns:IWebService_Instruction_OutputMessage"/>
</wsdl:operation>
<wsdl:operation name="GetUpdate">
<wsdl:input wsaw:Action="GetUpdate" message="tns:IWebService_GetUpdate_InputMessage"/>
<wsdl:output wsaw:Action="SMS_Application/IWebService/GetUpdateResponse" message="tns:IWebService_GetUpdate_OutputMessage"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:service name="Callback">
<wsdl:port name="WSHttpBinding_IWebService" binding="i0:WSHttpBinding_IWebService">
<soap12:address location="http://192.168.8.101:8733/SLWebServiceServer/Application"/>
<wsa10:EndpointReference>
<wsa10:Address>http://192.168.8.101:8733/SLWebServiceServer/Application</wsa10:Address>
<Identity xmlns="http://schemas.xmlsoap.org/ws/2006/02/addressingidentity">
<Upn>cuoghmar@slog.local</Upn>
</Identity>
</wsa10:EndpointReference>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>

如果需要更多信息,我可以添加它们。

我看到你已经在配置文件中配置了端点相关信息,所以我们不需要使用代码在ServiceHost中配置端点相关信息,我们只需要创建一个servicehost实例并启动它:

ServiceHost selfHost = new ServiceHost(typeof(Service1));
            selfHost.Open();
            Console.WriteLine("Service Open");
            Console.ReadLine();

这是我的文件目录:

我在ConsoleAPP1中引用了WcfserviceLibrary4。

最重要的一点是我们需要将WcfserviceLibrary4中的app.config复制到ConsoleAPP1中

如果问题仍然存在,请随时告诉我。