1,定义接口层,引用System.ServiceModel
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 |
namespace
Contracts { public
interface ICalculator { [OperationContract] double
Add( double
x, double
y); [OperationContract] double
Subtract( double
x, double
y); [OperationContract] double
Multiply( double
x, double
y); [OperationContract] double
Divide( double
x, double
y); } } |
2,实现接口层,增加接口层引用
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 |
namespace
Services { public
class CalculatorService : ICalculator { public
double Add( double
x, double
y) { return
x + y; } public
double Subtract( double
x, double
y) { return
x - y; } public
double Multiply( double
x, double
y) { return
x * y; } public
double Divide( double
x, double
y) { return
x / y; } } } |
3,创建wcf项目,增加配置信息
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
27
28 |
<system.serviceModel> <services> <service name= "Services.CalculatorService"
behaviorConfiguration= "SingleBehavior" > <endpoint binding= "basicHttpBinding"
bindingConfiguration= "SingleBinding"
contract= "Contracts.ICalculator" ></endpoint> </service> </services> <behaviors> <serviceBehaviors> <behavior name= "SingleBehavior" > <serviceMetadata httpGetEnabled= "true"
httpGetUrl= "" /> <serviceDebug includeExceptionDetailInFaults= "true" /> </behavior> </serviceBehaviors> </behaviors> <bindings> <basicHttpBinding> <binding name= "SingleBinding"
closeTimeout= "00:10:00" openTimeout= "00:10:00"
receiveTimeout= "00:10:00"
sendTimeout= "00:10:00" maxBufferSize= "2147483647"
maxBufferPoolSize= "2147483647"
maxReceivedMessageSize= "2147483647" allowCookies= "false"
bypassProxyOnLocal= "false"
hostNameComparisonMode= "StrongWildcard" messageEncoding= "Text"
textEncoding= "utf-8"
transferMode= "Buffered" useDefaultWebProxy= "true" > <readerQuotas maxDepth= "2147483647"
maxStringContentLength= "2147483647"
maxArrayLength= "2147483647" maxBytesPerRead= "2147483647"
maxNameTableCharCount= "2147483647"
/> </binding> </basicHttpBinding> </bindings> </system.serviceModel> |
Service.svc中指定Service
1 |
<%@ ServiceHost Language= "C#"
Debug= "true"
Service= "Services.CalculatorService"
%> |
4,创建服务端
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
27
28
29
30
31
32
33
34
35 |
namespace
Client { class
Program { static
void Main( string [] args) { var
c= new
Client(); var
testvalue= c.Add(1, 2); Console.WriteLine(testvalue); } } public
class Client : ClientBase<ICalculator>, ICalculator { public
double Add( double
x, double
y) { return
this .Channel.Add(x, y); } public
double Subtract( double
x, double
y) { return
this .Channel.Subtract(x, y); } public
double Multiply( double
x, double
y) { return
this .Channel.Multiply(x, y); } public
double Divide( double
x, double
y) { return
this .Channel.Divide(x, y); } } } |
配置文件编写
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 |
<?xml version= "1.0"
encoding= "utf-8"
?> <configuration> <startup> <supportedRuntime version= "v4.0"
sku= ".NETFramework,Version=v4.5"
/> </startup> <system.serviceModel> <client> <endpoint address= "http://wcf.test.com/Service.svc"
binding= "basicHttpBinding"
bindingConfiguration= "SingleBinding"
contract= "Contracts.ICalculator"
name= "SingleBinding"
/> </client> <bindings> <basicHttpBinding> <binding name= "SingleBinding"
maxBufferPoolSize= "2147483647"
maxReceivedMessageSize= "2147483647"
sendTimeout= "00:10:00"
receiveTimeout= "00:10:00"
> <readerQuotas maxDepth= "2147483647"
maxStringContentLength= "2147483647"
maxArrayLength= "2147483647"
maxBytesPerRead= "2147483647"
maxNameTableCharCount= "2147483647"
/> </binding> </basicHttpBinding> </bindings> </system.serviceModel> </configuration> |
原文:http://www.cnblogs.com/anbylau2130/p/3543212.html