1
// Copyright 2004-2008 Castle Project - http://www.castleproject.org/
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle
.Facilities
.WcfIntegration
.Tests
18 using System
.Collections
.Generic
;
19 using System
.ServiceModel
;
20 using Castle
.MicroKernel
.Registration
;
22 using Castle
.Windsor
.Installer
;
23 using Castle
.Facilities
.WcfIntegration
.Demo
;
24 using NUnit
.Framework
;
25 using Castle
.Facilities
.WcfIntegration
.Tests
.Behaviors
;
30 public class ServiceHostFixture
33 public void CanCreateServiceHostAndOpenHost()
35 using (new WindsorContainer()
36 .AddFacility
<WcfFacility
>()
37 .Register(Component
.For
<IOperations
>()
38 .ImplementedBy
<Operations
>()
39 .DependsOn(new { number = 42 }
)
40 .ActAs(new DefaultServiceModel().AddEndpoints(
41 WcfEndpoint
.BoundTo(new NetTcpBinding())
42 .At("net.tcp://localhost/Operations"))
46 IOperations client
= ChannelFactory
<IOperations
>.CreateChannel(
47 new NetTcpBinding(), new EndpointAddress("net.tcp://localhost/Operations"));
48 Assert
.AreEqual(42, client
.GetValueFromConstructor());
53 public void CanCreateServiceHostAndOpenHostFromConfiguration()
55 using (new WindsorContainer()
56 .AddFacility
<WcfFacility
>()
57 .Register(Component
.For
<UsingWindsor
>()
58 .DependsOn(new { number = 42 }
)
59 .ActAs(new DefaultServiceModel())
62 IAmUsingWindsor client
= ChannelFactory
<IAmUsingWindsor
>.CreateChannel(
63 new BasicHttpBinding(), new EndpointAddress("http://localhost:27198/UsingWindsor.svc"));
64 Assert
.AreEqual(42, client
.GetValueFromWindsorConfig());
69 public void CanCreateServiceHostAndOpenHostWithMultipleEndpoints()
71 using (new WindsorContainer()
72 .AddFacility
<WcfFacility
>()
73 .Register(Component
.For
<Operations
>()
74 .DependsOn(new { number = 42 }
)
75 .ActAs(new DefaultServiceModel().AddEndpoints(
76 WcfEndpoint
.ForContract
<IOperations
>()
77 .BoundTo(new NetTcpBinding())
78 .At("net.tcp://localhost/Operations"),
79 WcfEndpoint
.ForContract
<IOperationsEx
>()
80 .BoundTo(new BasicHttpBinding())
81 .At("http://localhost:27198/UsingWindsor.svc")
85 IOperations client
= ChannelFactory
<IOperations
>.CreateChannel(
86 new NetTcpBinding(), new EndpointAddress("net.tcp://localhost/Operations"));
87 Assert
.AreEqual(42, client
.GetValueFromConstructor());
89 IOperationsEx clientEx
= ChannelFactory
<IOperationsEx
>.CreateChannel(
90 new BasicHttpBinding(), new EndpointAddress("http://localhost:27198/UsingWindsor.svc"));
91 clientEx
.Backup(new Dictionary
<string, object>());
96 public void CanCreateServiceHostAndOpenHostWithRelativeEndpoints()
98 using (new WindsorContainer()
99 .AddFacility
<WcfFacility
>()
100 .Register(Component
.For
<Operations
>()
101 .DependsOn(new { number = 42 }
)
102 .ActAs(new DefaultServiceModel()
104 "net.tcp://localhost/Operations",
105 "http://localhost:27198/UsingWindsor.svc")
107 WcfEndpoint
.ForContract
<IOperations
>()
108 .BoundTo(new NetTcpBinding()),
109 WcfEndpoint
.ForContract
<IOperationsEx
>()
110 .BoundTo(new BasicHttpBinding())
116 IOperations client
= ChannelFactory
<IOperations
>.CreateChannel(
117 new NetTcpBinding(), new EndpointAddress("net.tcp://localhost/Operations"));
118 Assert
.AreEqual(42, client
.GetValueFromConstructor());
120 IOperationsEx clientEx
= ChannelFactory
<IOperationsEx
>.CreateChannel(
121 new BasicHttpBinding(), new EndpointAddress("http://localhost:27198/UsingWindsor.svc/Extended"));
122 clientEx
.Backup(new Dictionary
<string, object>());
127 public void CanCreateServiceHostAndOpenHostWithListenAddress()
129 using (new WindsorContainer()
130 .AddFacility
<WcfFacility
>()
131 .Register(Component
.For
<IOperations
>()
132 .ImplementedBy
<Operations
>()
133 .DependsOn(new { number = 42 }
)
134 .ActAs(new DefaultServiceModel().AddEndpoints(
135 WcfEndpoint
.BoundTo(new NetTcpBinding())
136 .At("urn:castle:operations")
137 .Via("net.tcp://localhost/Operations")
141 IOperations client
= ChannelFactory
<IOperations
>.CreateChannel(
142 new NetTcpBinding(), new EndpointAddress("urn:castle:operations"),
143 new Uri("net.tcp://localhost/Operations"));
144 Assert
.AreEqual(42, client
.GetValueFromConstructor());
149 public void CanCreateServiceHostAndOpenHostFromXmlConfiguration()
151 using (new WindsorContainer()
152 .Install(Configuration
.FromXmlFile("..\\..\\ConfigureServices.xml")))
154 IAmUsingWindsor client
= ChannelFactory
<IAmUsingWindsor
>.CreateChannel(
155 new BasicHttpBinding(), new EndpointAddress("http://localhost:27198/UsingWindsor.svc"));
156 Assert
.AreEqual(42, client
.GetValueFromWindsorConfig());
161 public void CanCreateServiceHostAndOpenHostWithMultipleServiceModels()
163 using (new WindsorContainer()
164 .AddFacility
<WcfFacility
>()
165 .Register(Component
.For
<IOperations
>()
166 .ImplementedBy
<Operations
>()
167 .DependsOn(new { number = 42 }
)
169 new DefaultServiceModel().AddEndpoints(
170 WcfEndpoint
.BoundTo(new NetTcpBinding())
171 .At("net.tcp://localhost/Operations")
173 new DefaultServiceModel()
175 "http://localhost:27198/UsingWindsor.svc")
177 WcfEndpoint
.ForContract
<IOperationsEx
>()
178 .BoundTo(new BasicHttpBinding())
184 IOperations client
= ChannelFactory
<IOperations
>.CreateChannel(
185 new NetTcpBinding(), new EndpointAddress("net.tcp://localhost/Operations"));
186 Assert
.AreEqual(42, client
.GetValueFromConstructor());
188 IOperationsEx clientEx
= ChannelFactory
<IOperationsEx
>.CreateChannel(
189 new BasicHttpBinding(), new EndpointAddress("http://localhost:27198/UsingWindsor.svc/Extended"));
190 clientEx
.Backup(new Dictionary
<string, object>());
195 public void WillApplyServiceScopedBehaviors()
197 CallCountServiceBehavior
.CallCount
= 0;
198 Assert
.IsFalse(UnitOfWork
.initialized
, "Should be false before starting");
200 using (new WindsorContainer()
201 .AddFacility
<WcfFacility
>()
203 Component
.For
<CallCountServiceBehavior
>()
204 .Configuration(Attrib
.ForName("scope").Eq(WcfBehaviorScope
.Clients
)),
205 Component
.For
<UnitOfworkEndPointBehavior
>()
206 .Configuration(Attrib
.ForName("scope").Eq(WcfBehaviorScope
.Services
)),
207 Component
.For
<IOperations
>().ImplementedBy
<Operations
>()
208 .DependsOn(new { number = 42 }
)
209 .ActAs(new DefaultServiceModel().AddEndpoints(
210 WcfEndpoint
.BoundTo(new NetTcpBinding())
211 .At("net.tcp://localhost/Operations"))
215 IOperations client
= ChannelFactory
<IOperations
>.CreateChannel(
216 new NetTcpBinding(), new EndpointAddress("net.tcp://localhost/Operations"));
217 bool unitOfWorkIsInitialized_DuringCall
= client
.UnitOfWorkIsInitialized();
218 Assert
.IsTrue(unitOfWorkIsInitialized_DuringCall
);
219 Assert
.IsFalse(UnitOfWork
.initialized
, "Should be false after call");
220 Assert
.AreEqual(0, CallCountServiceBehavior
.CallCount
);
225 public void WillApplyExplcitScopedKeyBehaviors()
227 CallCountServiceBehavior
.CallCount
= 0;
229 using (new WindsorContainer()
230 .AddFacility
<WcfFacility
>()
232 Component
.For
<CallCountServiceBehavior
>()
234 .Configuration(Attrib
.ForName("scope").Eq(WcfBehaviorScope
.Explicit
)),
235 Component
.For
<IOperations
>().ImplementedBy
<Operations
>()
236 .DependsOn(new { number = 42 }
)
237 .ActAs(new DefaultServiceModel()
239 WcfEndpoint
.BoundTo(new NetTcpBinding())
240 .At("net.tcp://localhost/Operations"))
241 .AddBehaviors("callcounts")
245 IOperations client
= ChannelFactory
<IOperations
>.CreateChannel(
246 new NetTcpBinding(), new EndpointAddress("net.tcp://localhost/Operations"));
247 Assert
.AreEqual(42, client
.GetValueFromConstructor());
248 Assert
.AreEqual(1, CallCountServiceBehavior
.CallCount
);
253 public void WillApplyExplcitScopedServiceBehaviors()
255 CallCountServiceBehavior
.CallCount
= 0;
257 using (new WindsorContainer()
258 .AddFacility
<WcfFacility
>()
260 Component
.For
<CallCountServiceBehavior
>()
261 .Configuration(Attrib
.ForName("scope").Eq(WcfBehaviorScope
.Explicit
)),
262 Component
.For
<IOperations
>().ImplementedBy
<Operations
>()
263 .DependsOn(new { number = 42 }
)
264 .ActAs(new DefaultServiceModel()
266 WcfEndpoint
.BoundTo(new NetTcpBinding())
267 .At("net.tcp://localhost/Operations"))
268 .AddBehaviors(typeof(CallCountServiceBehavior
))
272 IOperations client
= ChannelFactory
<IOperations
>.CreateChannel(
273 new NetTcpBinding(), new EndpointAddress("net.tcp://localhost/Operations"));
274 Assert
.AreEqual(42, client
.GetValueFromConstructor());
275 Assert
.AreEqual(1, CallCountServiceBehavior
.CallCount
);
280 public void WillApplyInstanceBehaviors()
282 CallCountServiceBehavior
.CallCount
= 0;
284 using (new WindsorContainer()
285 .AddFacility
<WcfFacility
>()
287 Component
.For
<IOperations
>().ImplementedBy
<Operations
>()
288 .DependsOn(new { number = 42 }
)
289 .ActAs(new DefaultServiceModel()
291 WcfEndpoint
.BoundTo(new NetTcpBinding())
292 .At("net.tcp://localhost/Operations"))
293 .AddBehaviors(new CallCountServiceBehavior(),
294 new UnitOfworkEndPointBehavior())
298 IOperations client
= ChannelFactory
<IOperations
>.CreateChannel(
299 new NetTcpBinding(), new EndpointAddress("net.tcp://localhost/Operations"));
300 bool unitOfWorkIsInitialized_DuringCall
= client
.UnitOfWorkIsInitialized();
301 Assert
.IsTrue(unitOfWorkIsInitialized_DuringCall
);
302 Assert
.IsFalse(UnitOfWork
.initialized
, "Should be false after call");
303 Assert
.AreEqual(1, CallCountServiceBehavior
.CallCount
);