繁体   English   中英

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

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

我正在努力找出问题,但我不能,而且我无法在谷歌上找到合适的解决方案。

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

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 的引用并启动该服务,则定义更改以及 URL,但是如果我将 WebService 项目添加到当前解决方案中,所有这些都将按预期工作。

我找不到问题,我认为这与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>

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

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

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

这是我的文件目录:

在此处输入图片说明

我在 ConsoleAPP1 中引用了 WcfserviceLibrary4。

在此处输入图片说明

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

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

暂无
暂无

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

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