-
Notifications
You must be signed in to change notification settings - Fork 17
/
#WCF Jumpstart=Brain Noyes;Note=Erxin.txt#
338 lines (253 loc) · 10.7 KB
/
#WCF Jumpstart=Brain Noyes;Note=Erxin.txt#
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
WCF Jumpstart=Brain Noyes;Note=Erxin
# Hello WCF
- Course Outline
- WCF Overview
+ windows communication foundation
+ platform for building distributed, service-oriented applications
* define services and hosts for those services
* define clients to connect to services
+ SOAP messaging, xml based message protocol, not required to http protocol
+ abstracts your code from wire-level protocol details
focus on business operations instead of plumbing
+ make it easy for .net object-oriented programmer to build distributed service-oritendted applications
* secure
* scalable
* robust
* flexible
- WCF Building Blocks
+ service contract
* data contract
* service
+ graphic
+--------------+ +--------------------+
|consumer | |service host |
| | | | +-----------------+|
| V | | |service contract ||
| proxy--------------------> ||
| | | | +-------------+ ||
| | | | |data contract| ||
| | | | +-------------+ ||
| | | +-----------------+|
| | | +-----------------+|
| | | |service ||
| | | +-----------------+|
| | | +-----------------+|
| configuration| | |configuration ||
| | | +-----------------+|
+--------------+ +--------------------+
- wcf configuration
+ endpoints, compose ABC,
* address, how client get to service
* binding, how communication works at wire level
* contract, defines what's expose
+ example of phone call
* number -> address
* conversation content -> contract
* same language -> binding
- Demo hello wcf service
+ debug start external program
- Demo hello wcf client
+ check the project option, wcf server option WCF Options| Start WCF Service Host when debugging another project in the same solution
- Demo service app solution tour
# Implementing Services
- Introduction
+ service implementation overview
+ define service contract
+ defining data contract
+ implementation
- Service implementation Overview
+ create a wcf service library project, for contain service definition
+ define service contract
+ define data contract for operation parameters and return types
+ define the service implementation class
service layer -> dispatch to business layer -> data layer method
- demo service contract
+ create a new wcf service library
+ delete automatic generated service file
+ remote the auto generated service configuration from app.config file
<services>
<service name ... ></service> //remove
</services>
+ go to WCF Options and turn off Start WCF Service Host when debugging another project in the same solution
+ go to Debug tab comment out the command line library that the wcf test client
--/client "WcfTestClient.exe"
+ additional reference of wcf compare to normal class library
System.Runtime.Serialization
System.SeriviceModel
+ add service contract
[ServiceContract]
public interface IZzaService
{
[OperationContract]
List<product> GetProducts();
}
- data contract
[DataContract]
public class product
{
[DataMemeber]
public int Id {get;set;}
}
with out data contract is implicit data contract, you don't have much control on wire level
with DataContract you could control the expose name, namespace etc.
[DataContract(NameSpace="...", Name="newName")]
- demo service implementation, service is a class implementation a interface
[ServiceBehavior(InstanceContextModel=InstanceContextMode.PerCall/PerSession/Single)]
public class ZzaService:IZzaService, IDisposable
{
readonly ZzaDbContext _Context = new ZzaDbContext();
public void Dispose()
{
_Context.Dispose();
}
[OperationBehavior(TransactionScopeRequired=true)]
public void SubmitOrder(Order order)
{
_Context.Orders.Add(order);
_Context.SaveChanges();
}
}
use entity framework data contract
define and implementation IDisposable to ask wcf host to dispose the service
use instanceContextMode to determine the lifetime of your service interface
the default is percall based wcf instance mode
wcf support transaction set TransactionScopeRequired behavior could ensure the data is inserted integrated
# Hosting Services
- introduction
- hosting overview
- service configuration, the most important and complex
- wcf service library hosting
+ self hosting
* any .net process
* use ServiceHost class
+ IIS Host
* hosted in a web site on an iis server
* defined through an asp.net web application project
* can only hand HTTP traffic
* Get the benefit of iis scalability, robustness, and configurability
* can handle other protocols through "WAS"
window process activation service
+ WCF service library
* ok for quick smoke testing
* generally want to debug and develop with a self or IIS host
- demo configuring endpoints
+ configuring endpoints, supply the protocol and address and port
<system.serviceModel>
<services>
<service name="Zza.Services.ZzaService">
<endpoint address="http://localhost:2112/Zza" binding="basicHttpBinding" contract="Zza.Services.IZzaSerivce"/>
<endpoint address="net.tcp://localhost:2113/Zza" binding="netTcpBinding" contract="Zza.Services.IZzaSerivce"/>
</service>
</services>
</system.serviceModel>
- demo, configuring behaviors
<system.serviceModel>
<services>
<host>
<baseAddresses>
//with base address could direct use relative address references
<add baseAddress="http://localhost:2112"/>
</baseAddresses>
</host>
<service name="Zza.Services.ZzaService">
<endpoint address="Zza" binding="basicHttpBinding" contract="Zza.Services.IZzaSerivce"/>
...
</service>
</services>
<behaviors>
//define a behavior without a name will apply it to all the service for the server
<behavior>
<serviceDebug includeExceptionDetaillInFaults="true"/>
//support get the wisdle information of the service
<serviceMetadata httpGetEnable="true"/>
</behavior>
</behaviors>
</system.serviceModel>
- demo configuring binding, every binding have lots of configuration aspect to drive the communication happen at wire level
Could use binding configuration to tweak the bindings
<system.serviceModel>
<services>
<service>
...
</service>
</services>
<behaviors>
//define a behavior without a name will apply it to all the service for the server
<behavior>
...
</behavior>
</behaviors>
<bindings>
<basicHttpBinding>
<binding maxReceivedMessageSize="21431" maxBufferSize="2141234">
<readerQuotas maxArrayLength="2147483647" maxStringContextLength="2147483647"/>
</binding>
</basicHttpBinding>
<netTcpBinding>
<binding maxReceivedMessageSize="21431" maxBufferSize="2141234">
<readerQuotas maxArrayLength="2147483647" maxStringContextLength="2147483647"/>
</binding>
</netTcpBinding>
</bindings>
</system.serviceModel>
- hosting in wcf service library
+ connection string configuration
<connectionStrings>
<add name="ZzaDbContext" connectionString="server=.;database=Zza;trusted_connection=true" providerName.../>
</connectionStrings>
<system.serviceModel>
<services>...</services>
<behaviors>...</behaviors>
<bindings>...</bindings>
</system.serviceModel>
+ uncomment the start wcf client command line from the project properties
+ check the wcf options start debugging
+ run the wcf service library
- hosting in console application
+ create a console applicatoin
+ reference the wcf service library
+ add reference to
System.ServiceModel
System.Runtime.Serialization
+ for the configuration, copy and past the connection string and service model sections
+ host the code
ServiceHost host = new ServiceHost(typeof(ZzaServie));
host.Open();
Console.ReadKey();
host.Close(); // graceful shutdown
both open and close may throw exceptions, sole need add them into try catch scope
- hosting-in iis
+ create a asp.net web project
+ reference wcf service library
+ copy and past the connection string and service model section
remote the basic address section and address property for the service element
<system.serviceModel>
<services>
<!--removed
<host>
<baseAddresses>
//with base address could direct use relative address references
<add baseAddress="http://localhost:2112"/>
</baseAddresses>
</host>-->
<service name="Zza.Services.ZzaService">
<endpoint binding="basicHttpBinding" contract="Zza.Services.IZzaSerivce"/>
...
</service>
</services>
+ add a serviceName.svc file with content
<%@ ServiceHost Service="TheServiceImplementationClass"%>
+ if you don't do the WAS hosting, then remove the other endpoint except basicHttpBinding
# Implementing client
- introduction
- wcf client overview
+ to consume wcf services from .net clients you need a client proxy
+ proxies can be code generated by visual studio based on metadata exposed by the service
* service must enable metadata either through wsdl or ws-metadataexchange endpoint
+ proxies can be hand-coded for more control over the code in proxy
* encapsulate repeating patterns of usage, leverage WCF extensibility features
+ make calls through proxy instance methods
* opens connection to the server
* dispatches the call through SOAP messages
* gets a response message back - completes method
- code generation