WCF命名管道最小示例

WCF命名管道最小示例

我正在寻找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>

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

// 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 ServiceHostServiceHost 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();}

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


白猪掌柜的
浏览 518回答 3
3回答

MMTTMM

我也遵循微软的教程,这很好,但我也只需要管道。如您所见,您不需要配置文件和所有杂乱的东西。顺便说一句,他使用HTTP和管道。只需删除与HTTP相关的所有代码行,您就会得到一个纯粹的管道示例。

慕无忌1623718

试试这个。这是服务部分。[ServiceContract]public&nbsp;interface&nbsp;IService{ &nbsp;&nbsp;&nbsp;&nbsp;[OperationContract] &nbsp;&nbsp;&nbsp;&nbsp;void&nbsp;&nbsp;HelloWorld();}public&nbsp;class&nbsp;Service&nbsp;:&nbsp;IService{ &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;void&nbsp;HelloWorld() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//Hello&nbsp;World &nbsp;&nbsp;&nbsp;&nbsp;}}这是代理public&nbsp;class&nbsp;ServiceProxy&nbsp;:&nbsp;ClientBase<IService>{ &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;ServiceProxy() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;base(new&nbsp;ServiceEndpoint(ContractDescription.GetContract(typeof(IService)), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new&nbsp;NetNamedPipeBinding(),&nbsp;new&nbsp;EndpointAddress("net.pipe://localhost/MyAppNameThatNobodyElseWillUse/helloservice"))) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;void&nbsp;InvokeHelloWorld() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Channel.HelloWorld(); &nbsp;&nbsp;&nbsp;&nbsp;}}这是服务托管部分。var&nbsp;serviceHost&nbsp;=&nbsp;new&nbsp;ServiceHost &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(typeof(Service),&nbsp;new&nbsp;Uri[]&nbsp;{&nbsp;new&nbsp;Uri("net.pipe://localhost/MyAppNameThatNobodyElseWillUse")&nbsp;}); &nbsp;&nbsp;&nbsp;&nbsp;serviceHost.AddServiceEndpoint(typeof(IService),&nbsp;new&nbsp;NetNamedPipeBinding(),&nbsp;"helloservice"); &nbsp;&nbsp;&nbsp;&nbsp;serviceHost.Open(); &nbsp;&nbsp;&nbsp;&nbsp;Console.WriteLine("Service&nbsp;started.&nbsp;Available&nbsp;in&nbsp;following&nbsp;endpoints"); &nbsp;&nbsp;&nbsp;&nbsp;foreach&nbsp;(var&nbsp;serviceEndpoint&nbsp;in&nbsp;serviceHost.Description.Endpoints) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Console.WriteLine(serviceEndpoint.ListenUri.AbsoluteUri); &nbsp;&nbsp;&nbsp;&nbsp;}
打开App,查看更多内容
随时随地看视频慕课网APP