zoukankan      html  css  js  c++  java
  • 几个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>

  • 相关阅读:
    在Ubuntu中通过update-alternatives切换软件版本
    SCons: 替代 make 和 makefile 及 javac 的极好用的c、c++、java 构建工具
    mongodb 的使用
    利用grub从ubuntu找回windows启动项
    How to Repair GRUB2 When Ubuntu Won’t Boot
    Redis vs Mongo vs mysql
    java script 的工具
    python 的弹框
    how to use greendao in android studio
    python yield的终极解释
  • 原文地址:https://www.cnblogs.com/jx270/p/3085496.html
Copyright © 2011-2022 走看看