Files
spring-net/doc/reference/src/wcf-quickstart.xml
sbohlen 9788fd3580 SPRNET-1407
Updated copyright notices in docs.
2010-12-11 20:35:49 +00:00

186 lines
7.0 KiB
XML

<?xml version="1.0" encoding="UTF-8"?>
<!--
/*
* Copyright 2002-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-->
<chapter version="5" xml:id="wcf-quickstart"
xmlns="http://docbook.org/ns/docbook"
xmlns:ns6="http://www.w3.org/1999/xlink"
xmlns:ns5="http://www.w3.org/2000/svg"
xmlns:ns4="http://www.w3.org/1999/xhtml"
xmlns:ns3="http://www.w3.org/1998/Math/MathML"
xmlns:ns="http://docbook.org/ns/docbook">
<title>WCF QuickStart</title>
<section>
<title>Introduction</title>
<para>The WCF quickstart application shows how to configure your WCF
services using dependency injection and how to to apply AOP advice to your
services. It is based on the same interfaces used in the <link
linkend="remoting-quickstart">portable service abstractions
quickstart</link> example that demonstrates similar features for .NET
Remoting, Enterprise Services, and ASMX web sevices. The quickstart
example is only available as a VS.NET 2008 solution.</para>
<para>There are two server applications in the solution, one is a web
application where the WCF service will be hosted, and the other is a
self-hosting console application, Spring.WcfQuickStart.Server.2008. The
client application is located in Sprng.WcfQuickStart.ClientApp.2008. To
run the solution make sure that all three projects are set to
startup.</para>
<note>
<para>To follow this Quarts QuickStart load the solution file found in
the directory
<literal>&lt;spring-install-dir&gt;\examples\Spring\Spring.WcfQuickStart</literal></para>
</note>
</section>
<section>
<title>The server side</title>
<para>The service contract is shown below</para>
<programlisting language="csharp"> [ServiceContract(Namespace = "http://Spring.WcfQuickStart")]
public interface ICalculator
{
[OperationContract]
double Add(double n1, double n2);
[OperationContract]
double Subtract(double n1, double n2);
[OperationContract]
double Multiply(double n1, double n2);
[OperationContract]
double Divide(double n1, double n2);
[OperationContract]
string GetName();
}</programlisting>
<para>and the implementation is straightforward, only adding a property
that controls how long each method should sleep. An abbreviated listing of
the implementation is shown below</para>
<programlisting language="csharp"> public class CalculatorService : ICalculator
{
private int sleepInSeconds;
public int SleepInSeconds
{
get { return sleepInSeconds; }
set { sleepInSeconds = value; }
}
public double Add(double n1, double n2)
{
Thread.Sleep(sleepInSeconds*1000);
return n1 + n2;
}
// other methods omitted for brevity.
}</programlisting>
<section>
<title>WCF Dependency Injection and AOP in self-hosted
application</title>
<para>The approach using dynamic proxies is used in the console
application inside the Spring.WcfQuickStart.Server.2008 project. For
more information on this approach refer to this <link
linkend="wcf-di-proxy">section</link> in the reference docs. The
configuration of your service is done as you would typically do with
Spring, including applying of any AOP advice. The class is hosted inside
the console application through the use of Spring's
<literal>ServiceHostFactoryObject</literal> exporter. The configuration
for the server console application is shown below.</para>
<programlisting language="myxml"> &lt;objects xmlns="http://www.springframework.net"
xmlns:aop="http://www.springframework.net/aop"&gt;
&lt;!-- Service definition --&gt;
&lt;object id="calculator" singleton="false"
type="Spring.WcfQuickStart.CalculatorService, Spring.WcfQuickStart.ServerApp"&gt;
&lt;property name="SleepInSeconds" value="1"/&gt;
&lt;/object&gt;
&lt;object id="serviceOperation" type="Spring.Aop.Support.SdkRegularExpressionMethodPointcut, Spring.Aop"&gt;
&lt;property name="pattern" value="Spring.WcfQuickStart.*"/&gt;
&lt;/object&gt;
&lt;object id="perfAdvice" type="Spring.WcfQuickStart.SimplePerformanceInterceptor, Spring.WcfQuickStart.ServerApp"&gt;
&lt;property name="Prefix" value="Service Layer Performance"/&gt;
&lt;/object&gt;
&lt;aop:config&gt;
&lt;aop:advisor pointcut-ref="serviceOperation" advice-ref="perfAdvice"/&gt;
&lt;/aop:config&gt;
&lt;!-- host the service object --&gt;
&lt;object id="calculatorServiceHost" type="Spring.ServiceModel.Activation.ServiceHostFactoryObject, Spring.Services"&gt;
&lt;property name="TargetName" value="calculator" /&gt;
&lt;/object&gt;
&lt;/objects&gt;</programlisting>
<para>Look at the standard WCF configuration section in App.config for
additional configuration details. In that section you will see that the
name of the WCF service corresponds to the name of the service object
inside the spring container.</para>
</section>
<section>
<title>WCF Dependency Injection and AOP in IIS web application</title>
<para>Much of the configuration ob the objects is the same as before,
the .svc file though refers to the name of the service inside the Spring
container as well as using Spring's
Spring.ServiceModel.Activation.ServiceHostFactory. The .svc file is
shown below.</para>
<programlisting language="myxml">&lt;%@ ServiceHost Language="C#" Debug="true" Service="calculator"
Factory="Spring.ServiceModel.Activation.ServiceHostFactory" %&gt;
</programlisting>
</section>
</section>
<section>
<title>Client access</title>
<para>The project Spring.WcfQuickStart.ClientApp.2008 is a console
application that calls the two WCF services. It creates a client side
proxy based on using ChannelFactory&lt;T&gt;.CreateChannel. Running the
client application produces the following output.</para>
<programlisting>--- Press &lt;return&gt; to continue ---
Web Calculator
Add(1, 1) : 2
Divide(11, 2) : 5.5
Multiply(2, 5) : 10
Subtract(7, 4) : 3
ServerApp Calculator
Add(1, 1) : 2
Divide(11, 2) : 5.5
Multiply(2, 5) : 10
Subtract(7, 4) : 3
--- Press &lt;return&gt; to continue ---
</programlisting>
</section>
</chapter>