我正在尝试在Windows服务中托管WCF net.pipe服务。我在Windows服务的OnStart()
函数中用代码定义服务。我也以类似的方式创建客户端 - 代码。
我已经看到了这个问题,但它似乎总是只用于在app / web.config文件中定义NetNamedPipe的情况。
尝试调用服务时,出现错误:
System.ServiceModel.ProtocolException: The requested upgrade is not supported by 'net.pipe://localhost/manager'. This could be due to mismatched bindings (for example security enabled on the client and not on the server).
Server stack trace:
at System.ServiceModel.Channels.ConnectionUpgradeHelper.DecodeFramingFault(ClientFramingDecoder decoder, IConnection connection, Uri via, String contentType, TimeoutHelper& timeoutHelper)
at System.ServiceModel.Channels.ClientFramingDuplexSessionChannel.SendPreamble(IConnection connection, ArraySegment`1 preamble, TimeoutHelper& timeoutHelper)
at System.ServiceModel.Channels.ClientFramingDuplexSessionChannel.DuplexConnectionPoolHelper.AcceptPooledConnection(IConnection connection, TimeoutHelper& timeoutHelper)
at System.ServiceModel.Channels.ConnectionPoolHelper.EstablishConnection(TimeSpan timeout)
at System.ServiceModel.Channels.ClientFramingDuplexSessionChannel.OnOpen(TimeSpan timeout)
at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
at System.ServiceModel.Channels.ServiceChannel.OnOpen(TimeSpan timeout)
at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
at System.ServiceModel.Channels.ServiceChannel.CallOnceManager.CallOnce(TimeSpan timeout, CallOnceManager cascade)
at System.ServiceModel.Channels.ServiceChannel.EnsureOpened(TimeSpan timeout)
at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)
Exception rethrown at [0]:
at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
at MyClientApp.IManager.HelloWorld()
这是我的代码:
//Service Contract:
[ServiceContract]
public interface IManager
{
[OperationContract]
string HelloWorld();
}
//Service
public class Manager : IManager
{
public string HelloWorld()
{
return "Hello to you too!";
}
}
//Defining and starting the Net.Pipe Service from the Windows Service
public partial class MyWindowsService : ServiceBase
{
public MyWindowsService()
{
InitializeComponent();
}
private ServiceHost m_serviceHost;
protected override void OnStart(string[] args)
{
try
{
m_serviceHost = new ServiceHost(typeof(Manager), new Uri("net.pipe://localhost"));
NetNamedPipeBinding binding = new NetNamedPipeBinding(NetNamedPipeSecurityMode.None);
m_serviceHost.AddServiceEndpoint(typeof(IManager), binding, "manager");
m_serviceHost.Open();
}
catch (Exception ex)
{
EventLog("MyWindowsService", ex.ToString(), EventLogEntryType.Error);
}
}
}
//The client proxy
public class ManagerProxy : ClientBase<IManager>
{
public ManagerProxy()
: base(new ServiceEndpoint(ContractDescription.GetContract(typeof(IManager)),
new NetNamedPipeBinding(), new EndpointAddress("net.pipe://localhost/manager"))) { }
public string InvokeHellowWorld()
{
return Channel.HelloWorld();
}
}
接口位于ClassLibrary项目中,并在主机应用程序(Windows服务)和尝试调用该服务的客户端应用程序之间共享。
Service类和OnStart函数位于Windows服务项目中。
服务代理位于客户端项目中(当然,它与Windows服务在同一台计算机上运行)。
此外,每当我尝试使用代理时,我都会发现它的State == CommunicationState.Faulted
。然后我关闭/中止它,并创建一个new ManagerProxy()
。然后ManagerProxy
的状态为Created
。我尝试调用HelloWorld
并获取上述Exception
下次我尝试使用它时 - 它的状态再次为Faulted
并重复该过程。
答案 0 :(得分:15)
我能看到的唯一区别是,在服务器端,您创建了明确没有安全模式binding = new NetNamedPipeBinding(NetNamedPipeSecurityMode.None);
的绑定,并且在客户端没有安全模式new NetNamedPipeBinding()
。异常消息确实提到安全性可能存在不匹配。
System.ServiceModel.ProtocolException:请求的升级不是 由net.pipe:// localhost / manager&#39;支持。这可能是由于 不匹配的绑定(例如在客户端上启用安全性和 不在服务器上。)
刚刚检查过here,默认安全模式不是NetNamedPipeSecurityMode.None
,而是NetNamedPipeSecurityMode.Transport
。所以那里存在不匹配。
答案 1 :(得分:1)
如果上述解决方案都不适合您,请尝试从端点中删除身份,如下所示:
<endpoint
address="net.tcp://127.0.0.1/FacilitySchedulesService/FacilitySchedulesService.svc"
binding="netTcpBinding"
bindingConfiguration="FacilityScheduleDSTCP"
contract="FacilitySchedules.IFacilitySchedulesService"
name="FacilityScheduleDSTCP">
<!--
<identity>
<userPrincipalName value="abc" />
</identity>
-->
</endpoint>
答案 2 :(得分:1)
另一种情况是,您为绑定配置中选择的安全模式使用了错误的子节点名称。在下面的示例中,尽管选择了传输模式,但使用了“消息”节点,从而导致“不支持请求的升级”异常:
<security mode="Transport">
<message clientCredentialType="Certificate"/>
</security>