![](/img/trans.png)
[英]WCF Service(NetTcpBinding) + host on managed WPF application
[英]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.