WCF命名管道最小示例

jav*_*red 89 c# wcf named-pipes

我正在寻找WCF命名管道的最小示例(我期望两个最小的应用程序,服务器和客户端,它们可以通过命名管道进行通信.)

Microsoft有一篇很好的文章" 入门教程",它通过HTTP描述了WCF,我正在寻找类似于WCF和命名管道的东西.

我在互联网上发现了几个帖子,但它们有点"先进".我需要一些最小的,只有强制性的功能,所以我可以添加我的代码并使应用程序正常工作.

如何替换它以使用命名管道?

<endpoint address="http://localhost:8000/ServiceModelSamples/Service/CalculatorService"
    binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_ICalculator"
    contract="ICalculator" name="WSHttpBinding_ICalculator">
    <identity>
        <userPrincipalName value="OlegPc\Oleg" />
    </identity>
</endpoint>
Run Code Online (Sandbox Code Playgroud)

如何替换它以使用命名管道?

// Step 1 of the address configuration procedure: Create a URI to serve as the base address.
Uri baseAddress = new Uri("http://localhost:8000/ServiceModelSamples/Service");

// Step 2 of the hosting procedure: Create ServiceHost
ServiceHost selfHost = new ServiceHost(typeof(CalculatorService), baseAddress);

try
{
    // Step 3 of the hosting procedure: Add a service endpoint.
    selfHost.AddServiceEndpoint(
        typeof(ICalculator),
        new WSHttpBinding(),
        "CalculatorService");

    // Step 4 of the hosting procedure: Enable metadata exchange.
    ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
    smb.HttpGetEnabled = true;
    selfHost.Description.Behaviors.Add(smb);

    // Step 5 of the hosting procedure: Start (and then stop) the service.
    selfHost.Open();
    Console.WriteLine("The service is ready.");
    Console.WriteLine("Press <ENTER> to terminate service.");
    Console.WriteLine();
    Console.ReadLine();

    // Close the ServiceHostBase to shutdown the service.
    selfHost.Close();
}
catch (CommunicationException ce)
{
    Console.WriteLine("An exception occurred: {0}", ce.Message);
    selfHost.Abort();
}
Run Code Online (Sandbox Code Playgroud)

如何生成客户端以使用命名管道?

Jua*_*uan 80

我刚刚发现这个优秀的小教程. 断链(缓存版)

我也遵循微软的教程,这很好,但我也只需要管道.

如您所见,您不需要配置文件和所有杂乱的东西.

顺便说一句,他使用HTTP和管道.只需删除与HTTP相关的所有代码行,您就会得到一个纯粹的管道示例.

  • 该链接不起作用,是其他地方的教程吗? (3认同)
  • 谢谢!此外,在尝试构建使用web.config进行配置而不是硬编码配置的服务时,请参阅此微软示例:http://msdn.microsoft.com/en-us/library/ms752253.aspx (2认同)

Anu*_*raj 58

试试这个.

这是服务部分.

[ServiceContract]
public interface IService
{
    [OperationContract]
    void  HelloWorld();
}

public class Service : IService
{
    public void HelloWorld()
    {
        //Hello World
    }
}
Run Code Online (Sandbox Code Playgroud)

这是代理

public class ServiceProxy : ClientBase<IService>
{
    public ServiceProxy()
        : base(new ServiceEndpoint(ContractDescription.GetContract(typeof(IService)),
            new NetNamedPipeBinding(), new EndpointAddress("net.pipe://localhost/MyAppNameThatNobodyElseWillUse/helloservice")))
    {

    }
    public void InvokeHelloWorld()
    {
        Channel.HelloWorld();
    }
}
Run Code Online (Sandbox Code Playgroud)

这是服务托管部分.

var serviceHost = new ServiceHost
        (typeof(Service), new Uri[] { new Uri("net.pipe://localhost/MyAppNameThatNobodyElseWillUse") });
    serviceHost.AddServiceEndpoint(typeof(IService), new NetNamedPipeBinding(), "helloservice");
    serviceHost.Open();

    Console.WriteLine("Service started. Available in following endpoints");
    foreach (var serviceEndpoint in serviceHost.Description.Endpoints)
    {
        Console.WriteLine(serviceEndpoint.ListenUri.AbsoluteUri);
    }
Run Code Online (Sandbox Code Playgroud)

  • 这是一个很好的例子,但是,永远不要使用net.pipe:// localhost /的基地址.如果你这样做,并且机器有任何其他程序也使用net.pipe:// localhost /那么ServiceHost会在你打开时抛出异常.相反,使用像net.pipe:// localhost/MyAppNameThatNobodyElseWillUse这样的独特内容.希望这有助于拯救别人一些时间和挫折! (12认同)
  • 很好,因为通过app.config文件公开应用程序详细信息通常是不可取的. (8认同)
  • 我需要将 `/helloservice` 添加到代理中端点地址的末尾。 (2认同)

小智 14

查看我的高度简化的Echo示例:它旨在使用基本的HTTP通信,但可以通过编辑客户端和服务器的app.config文件轻松修改它以使用命名管道.进行以下更改:

编辑服务器的app.config文件,删除或注释掉http baseAddress条目,并为命名管道添加新的baseAddress条目(称为net.pipe).此外,如果您不打算将HTTP用于通信协议,请确保已注释掉或删除了serviceMetadataserviceDebug:

<configuration>
    <system.serviceModel>
        <services>
            <service name="com.aschneider.examples.wcf.services.EchoService">
                <host>
                    <baseAddresses>
                        <add baseAddress="net.pipe://localhost/EchoService"/>
                    </baseAddresses>
                </host>
            </service>
        </services>
        <behaviors>
            <serviceBehaviors></serviceBehaviors>
        </behaviors>
    </system.serviceModel>
</configuration>
Run Code Online (Sandbox Code Playgroud)

编辑客户端的app.config文件,以便注释掉或删除basicHttpBinding,并添加netNamedPipeBinding条目.您还需要更改端点条目以使用管道:

<configuration>
    <system.serviceModel>
        <bindings>
            <netNamedPipeBinding>
                <binding name="NetNamedPipeBinding_IEchoService"/>
            </netNamedPipeBinding>
        </bindings>
        <client>
            <endpoint address              = "net.pipe://localhost/EchoService"
                      binding              = "netNamedPipeBinding"
                      bindingConfiguration = "NetNamedPipeBinding_IEchoService"
                      contract             = "EchoServiceReference.IEchoService"
                      name                 = "NetNamedPipeBinding_IEchoService"/>
        </client>
    </system.serviceModel>
</configuration>
Run Code Online (Sandbox Code Playgroud)

上面的示例只能使用命名管道运行,但没有什么能阻止您使用多个协议来运行您的服务.AFAIK,您应该能够让服务器使用命名管道和HTTP(以及其他协议)运行服务.

此外,客户端的app.config文件中的绑定也是高度简化的.除了指定baseAddress之外,您还可以调整许多不同的参数...

  • 链接现在已经死了. (4认同)