以前一直在点点晃荡,忽然想在Osc挖个坑~先把以前的一篇文章贴过来~ html
一、创建一个WebApplication解决方案,起个名字,直观点,就叫WCF web
2在项目中新建一个名叫test的wcf服务(ps:会同时生成一个itest的接口文件,能够无论他,这里是把这个接口文件删除了) api
三、新建Itest Itest2两个接口文件。 restful
四、标注接口为服务契约 ui
五、新建global文件 spa
Itest 调试
1
2
3
4
5
6
7
|
[ServiceContract(Namespace = "
http://localhost:13333/api/", Name = "Test")]
public interface Itest
{
[OperationContract(Name = "test1")]
[WebGet(BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml, UriTemplate = "/{para1}")]
string test1(string para1);
}
|
Itest2 rest
1
2
3
4
5
6
7
|
[ServiceContract(Namespace = "
http://localhost:13333/api/", Name = "testService")]
public interface Itest2
{
[OperationContract(Name = "test2")]
[WebGet(BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Xml, RequestFormat = WebMessageFormat.Xml, UriTemplate = "/{para2}")]
string test2(string para2);
}
|
ServiceContract这个特性声明当前接口为服务契约,OperationContract特性声明当前接口方法为操做契约(若是接口里多个方法,须要指定操做契约(OperationContract)的Name属性)。 orm
这里要注意,若是一个.svc文件要继承实现多个服务契约,必须为每一个契约(ServiceContract)指定Namespace Name属性(两个属性的值本身随意指定,但不能重复)。 htm
UriTemplate制定访问这个操做的地质,/{xxx}是要传入的参数,xxx名字必须和操做的参数相同。
test.svc(显式指定Namespace和Name是一个好习惯~)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
[ServiceBehavior(Name = "TestServiceHost", Namespace = "
http://localhost:13333/api/", ConcurrencyMode = ConcurrencyMode.Single, IncludeExceptionDetailInFaults = true, InstanceContextMode = InstanceContextMode.Single)]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class test : Itest, Itest2
{
#region Itest 成员
[OperationBehavior]
public string test1(string para1)
{
return para1;
}
#endregion
#region Itest 成员
[OperationBehavior]
public string test2(string para2)
{
return para2;
}
#endregion
}
|
web.config
在<configuration></configuration>跟节点中添加
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="wcf.testBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="restful">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="wcf.testBehavior" name="wcf.test">
<host>
<baseAddresses>
<add baseAddress="
http://localhost:8000/wcf/api/"/>
</baseAddresses>
</host>
<endpoint address="test1" binding="webHttpBinding" behaviorConfiguration="restful" contract="wcf.Itest"></endpoint>
<endpoint address="test2" binding="webHttpBinding" behaviorConfiguration="restful" contract="wcf.Itest2"></endpoint>
</service>
</services>
</system.serviceModel>
|
Global.asax
1
2
3
4
5
|
protected void Application_Start(object sender, EventArgs e){
test service = new test();
ServiceHost host = new ServiceHost(service);
host.Open();
}
|
这段代码是为了寄宿咱们的wcf服务
ok,搞定,调试下看看效果。
ps:win7下开发的时候,有时会报进程没有权限的错误,把vs关了,用管理员身份打开就ok了。
但愿本文对你们有用,搞定难题,心情舒畅,小lol两把,以慰生平~