• 几个WCF源码示例


    WCF_PRACTICE.rar

    以下插入(其中一个程序的)一些关键代码示例:

    服务端:

    定义接口


    namespace MathServiceLibrary
    {
    [ServiceContract]
    public interface IBasicMath
    {
    [OperationContract]
    int Add(int x, int y);
    }
    }

    继承接口


    namespace MathServiceLibrary
    {
    public class MathService : IBasicMath
    {
    public int Add(int x, int y)
    {
    // To simulate a lengthy request.
    System.Threading.Thread.Sleep(5000);
    return x + y;
    }
    }
    }


    using System.ServiceProcess;
    using System.Text;

    // Be sure to import these namespaces:
    using MathServiceLibrary;
    using System.ServiceModel;

    namespace MathWindowsServiceHost
    {
    public partial class MathWinService : ServiceBase
    {
    // A member variable of type ServiceHost.
    private ServiceHost myHost;

    public MathWinService()
    {
    InitializeComponent();
    }

    protected override void OnStart(string[] args)
    {
    if (myHost != null)
    {
    myHost.Close();
    }

    // Create the host and specify a URL for an HTTP binding.
    myHost = new ServiceHost(typeof(MathService),
    new Uri("http://localhost:8080/MathServiceLibrary"));
    myHost.AddDefaultEndpoints();

    // Open the host.
    myHost.Open();
    }

    protected override void OnStop()
    {
    // Shut down the host.
    if (myHost != null)
    myHost.Close();
    }
    }
    }

    客户端:

    using System;
    using MathClient.ServiceReference;
    using System.Threading;

    namespace MathClient
    {
    class Program
    {
    /* 在运行中,安装Windows服务的方法:
    //在运行中,将当前目录转向MathWindowsServiceHost.exe所在的目录,(盘符直接切换, cd 切换文件夹)
    //然后输入 F:\Windows\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe MathWindowsServiceHost.exe
    // 或F:\Windows\Microsoft.NET\Framework\v4.0.30319\IntallUtil.exe MathWindowsServiceHost.exe(本程序选4.0的)
    //则在服务中可以看到新增了一项服务

    //另外,删除服务的方法如下:
    //在运行中,将当前目录转向MathWindowsServiceHost.exe所在的目录,
    //sc delete MathWindowsServiceHost (MathWindowsServiceHost为当前要删除的服务名),即可删除该服务

    */

    static void Main(string[] args)
    {
    Console.WriteLine("***** The Async Math Client *****\n");

    using (BasicMathClient proxy = new BasicMathClient())
    {
    proxy.Open();

    // Add numbers in an async manner, using lambda expression
    IAsyncResult result = proxy.BeginAdd(2, 3,
    ar =>
    {
    Console.WriteLine("2 + 3 = {0}", proxy.EndAdd(ar));
    }, null);

    while (!result.IsCompleted)
    {
    Thread.Sleep(200);
    Console.WriteLine("Client working...");
    }
    }
    Console.ReadLine();
    }
    }
    }

    app.config :

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
    <system.serviceModel>
    <bindings>
    <basicHttpBinding>
    <binding name="BasicHttpBinding_IBasicMath" />
    </basicHttpBinding>
    <wsHttpBinding>
    <binding name="WorkflowControlHttpsBinding" transactionFlow="true">
    <security mode="Transport" />
    </binding>
    <binding name="WorkflowControlHttpBinding" transactionFlow="true" />
    </wsHttpBinding>
    </bindings>
    <client>
    <endpoint address="http://localhost:8080/MathServiceLibrary"
    binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IBasicMath"
    contract="ServiceReference.IBasicMath" name="BasicHttpBinding_IBasicMath" />
    </client>
    </system.serviceModel>
    </configuration>

  • 相关阅读:
    uvm设计分析——report
    report源码分析——report_handle和report_server和report_catcher
    report源码分析——宏的执行
    015-命令行下载安装brew
    011-多线程-基础-基于AbstractQueuedSynchronizer自定义同步组件
    010-多线程-JUC集合-Queue-ConcurrentLinkedQueue
    009-多线程-JUC集合-Queue-LinkedBlockingDeque
    008-多线程-JUC集合-Queue-LinkedBlockingQueue
    007-多线程-JUC集合-Queue-BlockingQueue接口以及ArrayBlockingQueue
    006-多线程-集合-Set-ConcurrentSkipListSet
  • 原文地址:https://www.cnblogs.com/jx270/p/3085496.html
Copyright © 2020-2023  润新知