端口未在WCF服务内打开

时间:2017-07-18 10:46:47

标签: .net wcf c++-cli port named-pipes

我有一个命名管道WCF服务。服务本身调用我没有权限的外部DLL代码。此代码的某些部分需要侦听特定的HTTP端口(我可以提供用于通信的IP和端口,但这些都是)。基本上,它设置状态监视器并期望从硬件设备的服务器进入端口的状态消息。

当要放入服务的代码在单独的独立程序中运行时,它可以正常工作,进行传入连接。但是当在服务中运行时,某些东西阻止了对该端口的访问。不是防火墙,已经检查过。我尝试使用以下方式获取端口访问权限:

auto perms = gcnew SocketPermission(NetworkAccess::Accept, TransportType::All, ip, port);
perms->Demand();

但它没有帮助。分析网络流量表明远程方尝试重复发送数据包,但服务没有看到并接受它们。

一些代码(虽然我不认为这次真的会增加上下文):

static void Main(array<String^>^ args) {
  array<ServiceBase^>^ ServicesToRun = gcnew array<ServiceBase^> { gcnew Service() };
  ServiceBase::Run(ServicesToRun);
}

virtual void OnStart(array<System::String^>^ args) override {
  Hosts->Clear();

  try {
    auto binding = gcnew NetNamedPipeBinding();
    binding->MaxReceivedMessageSize = Int32::MaxValue;
    binding->ReceiveTimeout = TimeSpan::MaxValue;

    Hosts->Add(AddHost(Test::typeid, ITest::typeid, binding, "net.pipe://localhost/service/test"));
  }
  catch (Exception^ e) {
    Log::Debug(e);
  }
}

ServiceHost^ AddHost(Type^ serviceClass, Type^ interfaceClass, Binding^ binding, String^ address) {
  auto host = gcnew ServiceHost(serviceClass);
  host->AddServiceEndpoint(interfaceClass, binding, address);
  host->Open();
  return host;
}

public interface class ITest {
  [OperationContract] void TestMonitor();
};

void Test::TestMonitor() {
  auto perms = gcnew SocketPermission(NetworkAccess::Accept, TransportType::All, ip, port);
  perms->Demand();
  Log::Debug("OK so far");

  // This external function is supposed to register the callback function on a server
  // and send regular status reports to ip:port. It fails because the service
  // is not allowed to listen on the port.
  StartMonitor(ip, port, callback);
  Log::Debug("never reached, the previous line times out");
}

0 个答案:

没有答案