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

问题描述

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

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

Update()

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

如果我在Visual Studio的同一解决方案(因此有两个项目,一个解决方案)中创建了一个具有主机(使用<?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> )的控制台应用程序,并且启动了该控制台应用程序,则所有工作均按预期进行。 问题是我有一个很大的应用程序,在多个解决方案中有多个DLL,我想要的是在一个DLL中添加对WebService的引用并启动自托管。但是,如果我添加对WebService的引用并启动服务,定义cahnge以及URL,但是如果我将WebService项目添加到当前解决方案中,则所有操作都将按预期进行。

我找不到问题,我认为这与ServiceHost文件有关,但是我找不到原因。

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

App.config

这是它与具有两个项目的解决方案一起创建的WSDL文件

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();
        }

,只有一个项目,而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: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>

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

解决方法

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

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

这是我的文件目录:

enter image description here

我在ConsoleAPP1中引用了WcfserviceLibrary4。

enter image description here

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

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

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...