gpt4 book ai didi

c# - "net.pipe://"不支持请求的升级

转载 作者:太空狗 更新时间:2023-10-29 23:59:48 26 4
gpt4 key购买 nike

我正在尝试在 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();
}
}

接口(interface)位于 ClassLibrary 项目中,并在主机应用程序(Windows 服务)和尝试调用该服务的客户端应用程序之间共享。

Service 类和 OnStart 函数在 Windows Service 项目中。

服务代理位于客户端项目中(当然,它与 Windows 服务在同一台机器上运行)。

此外,每当我尝试使用代理时,我都会发现它的 State == CommunicationState.Faulted。然后我关闭/中止它,并创建一个 new ManagerProxy()ManagerProxy 的状态是 Created。我尝试调用 HelloWorld 并得到上面的 Exception
下次我尝试使用它时 - 它的状态再次为 Faulted 并重复该过程。

最佳答案

我能看到的唯一区别是,在服务器端你创建的绑定(bind)没有明确的安全模式 binding = new NetNamedPipeBinding(NetNamedPipeSecurityMode.None); 而在客户端没有安全模式新的 NetNamedPipeBinding()。异常消息确实提到安全性可能不匹配。

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).

刚检查过here并且默认安全模式不是 NetNamedPipeSecurityMode.None,而是 NetNamedPipeSecurityMode.Transport。所以那里不匹配。

关于c# - "net.pipe://"不支持请求的升级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28041091/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com