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>

  • 相关阅读:
    [C#菜鸟]C# Hook (三) Windows常用消息大全
    [C#菜鸟]C# Hook (二) 常用钩子的类型
    C# hook WndProc
    Win7、win8、win10下实现精准截获Explorer拷贝行为
    对“XXX::Invoke”类型的已垃圾回收委托进行了回调。这可能会导致应用程序崩溃、损坏和数据丢失。向非托管代码传递委托时,托管应用程序必须让这些委托保持活动状态,直到确信不会再次调用它们
    VC中MessageBox与AfxMessageBox用法与区别
    C# 强命名程序集,防止dll被修改,混淆下发布
    easyhook报错The given 64-Bit library does not exist
    easyhook源码分析三——申请钩子
    Ext3日记文件系统为什么文件系统还会损坏?
  • 原文地址:https://www.cnblogs.com/jx270/p/3085496.html
Copyright © 2011-2022 走看看