以前一直在點點晃盪,忽然想在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兩把,以慰生平~