弹指一挥间

好好做事,学习待人 (大数据分析/.NET/JAVA)技术交流QQ:860280456; .NET/JAVA技术交流群:192028174

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

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>

posted on 2013-05-18 15:15  v.e.n.u.s  阅读(1182)  评论(0编辑  收藏  举报