C# 创建一个WCF服务

时间:2023-02-05 10:03:47

做代码统计,方便以后使用:

app.config配置文件设置:

<configuration>
<system.serviceModel>
<bindings>
<webHttpBinding>
<binding name="webBinding" maxBufferPoolSize="" maxBufferSize="" maxReceivedMessageSize="">
<readerQuotas maxDepth="" maxStringContentLength="" maxArrayLength="" maxBytesPerRead="" maxNameTableCharCount=""/>
</binding>
</webHttpBinding>
</bindings> <behaviors>
<serviceBehaviors>
<behavior name="mySerBeh">
<serviceMetadata httpGetEnabled="true"/>
<!--httpGetUrl="mex"-->
<!-- 要接收故障异常详细信息以进行调试,请将以下值设置为 true。在部署前设置为 false 以避免泄漏异常信息-->
<serviceDebug httpHelpPageEnabled="true" includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="webHttpendBehavior">
<webHttp></webHttp>
</behavior>
</endpointBehaviors>
</behaviors> <!-- 看到services节,就表明这是在定义服务相关的内容 -->
<services>
<!-- 定义一个服务,name是契约实现类的全名 -->
<service behaviorConfiguration="mySerBeh" name="WCFExample.WCF.UserService">
<host>
<baseAddresses>
<add baseAddress="http://127.0.0.1:21467/"/>
</baseAddresses>
</host>
<!-- 定义一下终节点,address一般为空,如果不为空,最终服务地址就是在baseAddress的基础上加上这个address,binding指定为basicHttpBinding,
这是最基础的基于http的绑定方式,contract标明这是为哪个契约服务 -->
<endpoint address="wcfs" behaviorConfiguration="webHttpendBehavior"
binding="webHttpBinding" bindingConfiguration="webBinding" contract="WCFExample.WCF.IService"></endpoint>
</service>
</services>
</system.serviceModel>
</configuration>

基本内容可以直接创建一个wpf服务会生成基本内容,服务分成两个,一个去做接口,一个去实现接口:

接口类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ServiceModel.Web; namespace WCFExample.WCF
{ //需要引用类库 System.ServiceModel 和 System.ServiceModel.Web
[ServiceContract]
public interface IService
{
[OperationContract]
[WebInvoke(Method = "GET", UriTemplate = "GetCon?op={name}", ResponseFormat = WebMessageFormat.Json)]
string GetCon(string name);
}
}

实现方法类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text; namespace WCFExample.WCF
{
/// <summary>
/// 用ServiceBehavior为契约实现类标定行为属性,此处指定并发模型为ConcurrencyMode.Multiple,即并发访问
/// </summary>
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class UserService : IService
{
public string GetCon(string name)
{
return name;
}
}
}

启动WCF类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.Threading; namespace WCFExample.WCF
{
public class WCFService
{ public static void Begion()
{
//无配置文件App.config的情况下手动绑定
//Uri HttpUri = new Uri("Http://localhost:21467/wcf");
//Type Servicetype = typeof(WCFExample.WCF.UserService);
//using (ServiceHost Chost=new ServiceHost (Servicetype,new Uri[]{HttpUri}))
//{
// Binding basicHttpBinding = new BasicHttpBinding();
// string address = "";
//Chost.AddServiceEndpoint(typeof(WCFExample.WCF.UserService), basicHttpBinding, address); // Chost.Open();
// Console.WriteLine("Service Running...");
// Console.ReadKey(true);
// Chost.Close();
//} //定义一个ServiceHost,注意参数中要使用契约实现类而不是接口
ServiceHost host = new ServiceHost(typeof(WCFExample.WCF.UserService));
host.Open();
while (true)
Thread.Sleep();
}
}
}

服务启动后,即可正常使用WCF服务