Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Windows服务中使用App.Config的WCF命名管道_C#_Wcf_App Config_Named Pipes - Fatal编程技术网

C# Windows服务中使用App.Config的WCF命名管道

C# Windows服务中使用App.Config的WCF命名管道,c#,wcf,app-config,named-pipes,C#,Wcf,App Config,Named Pipes,我很沮丧。好的,这里是错误 在网络上没有端点侦听。pipe://localhost/MyIpcAppToService 这可以接受这个信息。这通常是由不正确的地址或SOAP操作引起的。有关更多详细信息,请参阅InnerException(如果存在) 我终于让App.Config文件工作了,至少没有抱怨 当前App.Config <?xml version="1.0" encoding="utf-8"?> <configuration> <!-- When d

我很沮丧。好的,这里是错误

在网络上没有端点侦听。pipe://localhost/MyIpcAppToService 这可以接受这个信息。这通常是由不正确的地址或SOAP操作引起的。有关更多详细信息,请参阅InnerException(如果存在)

我终于让App.Config文件工作了,至少没有抱怨

当前App.Config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <!-- 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. -->
    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.2"/>
    </startup>
    <system.serviceModel>
        <services>
            <service behaviorConfiguration="MyServiceBehavior" name="MyService.Communication.IpcAppToService">
                <endpoint address="net.pipe://localhost/MyIpcAppToService" binding="wsDualHttpBinding" bindingConfiguration="MyAppToServiceEndpointBinding" contract="MyIpc.IIpcAppToService"/>
                <endpoint address="mex" binding="mexHttpBinding" name="mex" contract="IMetadataExchange"/>
                <host>
                    <baseAddresses>
                        <add baseAddress="http://localhost:8733/MyService/"/>
                    </baseAddresses>
                </host>
            </service>
  </services>
        <behaviors>
            <serviceBehaviors>
                <behavior name="MyServiceBehavior">
                    <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
                    <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
                    <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment  to avoid disclosing exception information -->
                    <serviceDebug includeExceptionDetailInFaults="true"/>
                    <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
                </behavior>
            </serviceBehaviors>
        </behaviors>
        <protocolMapping>
            <add scheme="http" binding="wsHttpBinding" bindingConfiguration="MyAppToServiceEndpointBinding" />
        </protocolMapping>
        <bindings>
            <wsDualHttpBinding>
                <!-- https://docs.microsoft.com/en-us/dotnet/framework/configure-apps/file-schema/wcf/wshttpbinding -->
                <binding name="MyAppToServiceEndpointBinding"
                                 transactionFlow="true"
                                 sendTimeout="00:01:00"
                                 maxReceivedMessageSize="2147483647"
                                 messageEncoding="Mtom">
                </binding>
            </wsDualHttpBinding>
        </bindings>
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true">
            <baseAddressPrefixFilters>
                <add prefix="http://localhost:8733"/>
            </baseAddressPrefixFilters>
        </serviceHostingEnvironment>
    </system.serviceModel>
    <appSettings>
        <add key="countoffiles" value="7"/>
        <add key="logfilelocation" value="abc.txt"/>
    </appSettings>
</configuration>
我读到该服务将自动启动放置在
App.Config
文件中的服务,实际上是
MyExeName.exe.Config
文件。我一直在看代码,发现它几乎是一样的,所以我用
net.pipe://
替换了
http://

可悲的是,旧代码,新代码,在代码之间,什么都没有。我一直收到同样的错误

我使用以下命令从桌面应用程序连接到服务

public static Boolean ConnectToService()
{
    try
    {
        var callback = new IpcCallbackAppToService();
        var context = new InstanceContext(callback);
        var pipeFactory = new DuplexChannelFactory<IIpcAppToService>(context, new NetNamedPipeBinding(), new EndpointAddress("net.pipe://localhost/MyIpcAppToService"));
        Program.HostIpcAppToService = pipeFactory.CreateChannel();
        Program.HostIpcAppToService.Connect();
        CommAppToService.IsPipeAppToService = true;

        return true;
    }

    catch (Exception ex)
    {
        // Log the exception.
        Errors.LogException(ex);
    }

    return false;
}
服务:

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class IpcAppToService : IIpcAppToService, IErrorHandler
{
    public static IIpcCallbackAppToService Callback { get; set; } = null;

    public void OpenCallback()
    {
        IpcAppToService.Callback = OperationContext.Current.GetCallbackChannel<IIpcCallbackAppToService>();
    }

    public void CloseCallback()
    {
        IpcAppToService.Callback = null;
    }

    public void SendMessage(string message)
    {
        //MessageBox.Show(message);
    }

    public UInt16 GetServiceId()
    {
        return Constants.My_Id_AppToService;
    }

    ...
}
修改后的客户端C#代码:

var callback = new IpcCallbackAppToService();
InstanceContext context = new InstanceContext(callback);
NetNamedPipeBinding binding = new NetNamedPipeBinding();
binding.Security.Mode = NetNamedPipeSecurityMode.None;
EndpointAddress endpointAddress = new EndpointAddress("net.pipe://localhost/MyIpcAppToService");
var pipeFactory = new DuplexChannelFactory<IIpcAppToService>(context, binding, endpointAddress);
Program.HostIpcAppToService = pipeFactory.CreateChannel();
Program.HostIpcAppToService.Connect();
CommAppToService.IsPipeAppToService = true;
var callback=new-IpcCallbackAppToService();
InstanceContext上下文=新InstanceContext(回调);
NetNamedPipeBinding=新的NetNamedPipeBinding();
binding.Security.Mode=NetNamedPipeSecurityMode.None;
EndpointAddress EndpointAddress=新的EndpointAddress(“网络地址”)。pipe://localhost/MyIpcAppToService");
var pipeFactory=新的DuplexChannelFactory(上下文、绑定、端点地址);
Program.HostIpcAppToService=pipeFactory.CreateChannel();
Program.HostIpcAppToService.Connect();
CommAppToService.IsPipeAppToService=true;
由于EventViewer是干净的,所以该服务不会抛出任何我可以检测到的异常,只有OnStart()成功完成的消息。我知道系统会处理
App.config
文件,就像以前我出错时一样,
Windows事件查看器会不断抱怨,但不会再抱怨了

以下是我使用的一些Microsoft文档:

我确实尝试过,但为
文件流
管道侦听器
管道监视器
指定了
\.\pipe\MyIpcToService
,但没有显示任何内容,即使我尝试使用WinForms桌面应用程序进行连接,该应用程序会抛出“未找到管道侦听器”异常

有什么问题吗

<endpoint address="net.pipe://localhost/MyIpcAppToService" binding="wsDualHttpBinding" bindingConfiguration="MyAppToServiceEndpointBinding" contract="MyIpc.IIpcAppToService"/>
Web.config(服务器端)

客户端app.config(自动生成)
我使用http地址(服务元数据获取地址)生成配置

  <system.serviceModel>
        <bindings>
            <netNamedPipeBinding>
                <binding name="NetNamedPipeBinding_IService1">
                    <security mode="None" />
                </binding>
            </netNamedPipeBinding>
        </bindings>
        <client>
            <endpoint address="net.pipe://mynetpipe/Service1.svc/MyIpcAppToService"
                binding="netNamedPipeBinding" bindingConfiguration="NetNamedPipeBinding_IService1"
                contract="ServiceReference1.IService1" name="NetNamedPipeBinding_IService1" />
        </client>
    </system.serviceModel>


如果有什么我可以帮忙的,请随时告诉我。

那么,是否存在InnerException?@stuartd在本例中有,但它说的是一样的:“管道端点”网络。pipe://localhost/MyIpcAppToService在本地计算机上找不到。“右键单击我的客户端应用程序并选择添加>添加服务引用和粘贴”net。pipe://localhost/MyIpcAppToService/mex“产生相同的消息,暗示服务器端出现问题。我实现了您的建议,请参阅我更新的问题,正好相反,即服务器端的App.config和客户端的C#。我仍然会犯同样的错误。IO Ninja,并不是说我是该工具的专家,对调试毫无帮助。我在服务器上的App.config中注释掉了该服务,并在C#代码中添加了该服务。我在客户端保留了C#代码,收到了相同的错误。有趣的是“没有端点侦听”的第二部分,即“可以接受消息”。这可能意味着客户端出现问题,而服务器端没有问题。我会调查的。在我看来,通过NetPipe发布服务有问题。我通过修改我的示例更新了我的回复,希望它对您有用。虽然我不得不在Windows服务器上使用C,而不是在
App.config
上使用C,但我在没有附加屏幕截图的情况下使服务正常工作。我无法使服务与配置文件一起工作。现在我正在写,也许我需要创建一个.SVC文件,而不仅仅是4个C#文件。它正在工作。非常感谢。附加信息是信息性的。
<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <!-- 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. -->
    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.2"/>
    </startup>
    <system.serviceModel>
        <services>
            <service behaviorConfiguration="BehaviorMyService" name="MyService.Communication.IpcAppToService">
                <endpoint address="net.pipe://localhost/MyIpcAppToService"
                                    binding="netNamedPipeBinding"
                                    bindingConfiguration="EndpointBindingMyAppToService"
                                    contract="MyIpc.IIpcAppToService"
                                    />
                <endpoint address="mex" binding="mexHttpBinding" name="mex" contract="IMetadataExchange"/>
                <host>
                    <baseAddresses>
                        <add baseAddress="http://localhost:8733/MyService/"/>
                    </baseAddresses>
                </host>
            </service>
        </services>
        <behaviors>
            <serviceBehaviors>
                <behavior name="BehaviorMyService">
                    <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
                    <serviceMetadata httpGetEnabled="true"
                                                     httpsGetEnabled="true"
                                                     />
                    <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment  to avoid disclosing exception information -->
                    <serviceDebug includeExceptionDetailInFaults="true"/>
                    <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
                </behavior>
            </serviceBehaviors>
        </behaviors>
        <bindings>
            <netNamedPipeBinding>
                <!-- https://docs.microsoft.com/en-us/dotnet/framework/configure-apps/file-schema/wcf/wshttpbinding -->
                <binding name="EndpointBindingMyAppToService"
                                 closeTimeout="00:01:00"  
                                 openTimeout="00:01:00"   
                                 receiveTimeout="00:10:00"   
                                 sendTimeout="00:01:00"  
                                 transactionFlow="false"   
                                 transferMode="Buffered"   
                                 transactionProtocol="OleTransactions"  
                                 hostNameComparisonMode="StrongWildcard"   
                                 maxBufferPoolSize="524288"  
                                 maxBufferSize="65536"   
                                 maxConnections="10"   
                                 maxReceivedMessageSize="2147483647"
                                 >
                    <security mode="None">
                        <transport protectionLevel="None" />
                    </security>
                </binding>
            </netNamedPipeBinding>
        </bindings>
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true">
            <baseAddressPrefixFilters>
                <add prefix="http://localhost:8733"/>
            </baseAddressPrefixFilters>
        </serviceHostingEnvironment>
    </system.serviceModel>
    <appSettings>
        <add key="countoffiles" value="7"/>
        <add key="logfilelocation" value="abc.txt"/>
    </appSettings>
</configuration>
var callback = new IpcCallbackAppToService();
InstanceContext context = new InstanceContext(callback);
NetNamedPipeBinding binding = new NetNamedPipeBinding();
binding.Security.Mode = NetNamedPipeSecurityMode.None;
EndpointAddress endpointAddress = new EndpointAddress("net.pipe://localhost/MyIpcAppToService");
var pipeFactory = new DuplexChannelFactory<IIpcAppToService>(context, binding, endpointAddress);
Program.HostIpcAppToService = pipeFactory.CreateChannel();
Program.HostIpcAppToService.Connect();
CommAppToService.IsPipeAppToService = true;
<endpoint address="net.pipe://localhost/MyIpcAppToService" binding="wsDualHttpBinding" bindingConfiguration="MyAppToServiceEndpointBinding" contract="MyIpc.IIpcAppToService"/>
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        string GetData(int value);
}
    public class Service1 : IService1
    {
        public string GetData(int value)
        {
            return string.Format("You entered: {0}", value);
        }
}
<system.serviceModel>
    <services>
      <service behaviorConfiguration="BehaviorMyService" name="WcfService1.Service1">
        <endpoint address="MyIpcAppToService"
                            binding="netNamedPipeBinding"
                            bindingConfiguration="EndpointBindingMyAppToService"
                            contract="WcfService1.IService1"
                                    />
        <endpoint address="mex" binding="mexHttpBinding" name="mex" contract="IMetadataExchange"/>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="BehaviorMyService">
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
          <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <bindings>
      <netNamedPipeBinding>
        <binding name="EndpointBindingMyAppToService"
                         closeTimeout="00:01:00"
                         openTimeout="00:01:00"
                         receiveTimeout="00:10:00"
                         sendTimeout="00:01:00"
                         transactionFlow="false"
                         transferMode="Buffered"
                         transactionProtocol="OleTransactions"
                         hostNameComparisonMode="StrongWildcard"
                         maxBufferPoolSize="524288"
                         maxConnections="10"
                         maxReceivedMessageSize="2147483647"
                                 >
          <security mode="None">
            <transport protectionLevel="None" />
          </security>
        </binding>
      </netNamedPipeBinding>
    </bindings>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true">
    </serviceHostingEnvironment>
  </system.serviceModel>
  ServiceReference1.Service1Client client = new ServiceReference1.Service1Client();
    var result = client.GetData(34);
    Console.WriteLine(result);
  <system.serviceModel>
        <bindings>
            <netNamedPipeBinding>
                <binding name="NetNamedPipeBinding_IService1">
                    <security mode="None" />
                </binding>
            </netNamedPipeBinding>
        </bindings>
        <client>
            <endpoint address="net.pipe://mynetpipe/Service1.svc/MyIpcAppToService"
                binding="netNamedPipeBinding" bindingConfiguration="NetNamedPipeBinding_IService1"
                contract="ServiceReference1.IService1" name="NetNamedPipeBinding_IService1" />
        </client>
    </system.serviceModel>