Introduction
Microsoft.NET 4.0 and Visual Studio.NET 2010 ships a lot of new
features in their underlying technologies. In this series of articles, I
want to talk about the new features in the area of Windows
Communication Foundation (WCF) in order to improve the development
experience, enable more communication scenario, support new WS-*
standards and provide a good integration with Windows Workflow
Foundation (WF).
The new features are essentially the following: simplified
configuration, standard endpoints, IIS hosting without a SVC file,
support for WS-Discovery, routing service, REST improvements,
enhancements for the integration with WF to support workflow services,
simple byte stream encoding and ETW tracing.
In this series of article, I will illustrate each feature
explaining the principles and showing some examples.
Simplified configuration
In WCF 3.x, when you specify a host for a Web service developed
using WCF, you need to write or program the endpoints and behaviors. In
WCF 4.0, you have new defaults for endpoints, binding and behaviors.
For our example, let's define a simple service which provides the
echo functionality when it's invoked. The service contract is shown in
the Listing 1.
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Runtime.Serialization;
using
System.ServiceModel;
using
System.Text;
namespace
WCF_NewFeatures
{
[ServiceContract
]
public
interface
IEchoService
{
[OperationContract
]
string
Echo(String
message);
}
}
Listing 1
The implementation for this service contract is shown in the
Listing 2.
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Runtime.Serialization;
using
System.ServiceModel;
using
System.Text;
namespace
WCF_NewFeatures
{
public
class
EchoService
: IEchoService
{
public
string
Echo(String
message)
{
return
"Called the
Echo Service with message "
+ message;
}
}
}
Listing 2
Now let's create a ServiceHost instance for this service and bind
it to a URI. When the application calls to the Open method of the
ServiceHost instance, it builds the internal service description from
the application configuration file along with anything the host
application may have configured explicitly (see Listing 3).
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.ServiceModel;
using
System.ServiceModel.Description;
namespace
WCF_NewFeatures
{
class
Program
{
static
void
Main(string
[]
args)
{
ServiceHost
serviceHost = new
ServiceHost
(typeof
(WCF_NewFeatures.EchoService
),new
Uri
("http://localhost:8080/Services/EchoService"
));
serviceHost.Open();
System.Console
.WriteLine("The
EchoService has started"
);
foreach
(ServiceEndpoint
se in
serviceHost.Description.Endpoints)
{
System.Console
.WriteLine("Address:{0},
Binding:{1}, Contract:{2}"
, se.Address, se.Binding.Name,
se.Contract.Name);
}
System.Console
.WriteLine("Please,
press any key to finish ..."
);
System.Console
.ReadLine();
serviceHost.Close();
}
}
}
Listing 3
When you run this program, you will see that the IEchoService
service contract can be accessed from the
http://localhost:8080/Services/EchoService using BasicHttpBinding (see
Figure 1). Notice that WCF uses the BasicHttpBinding as the default
binding.
Figure 1
If you configure at least one endpoint, you will no longer see any
default endpoint. Let's illustrate this by calling the
AddServiceEndpoint method in the ServiceHost instance (see Listing 4).
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.ServiceModel;
using
System.ServiceModel.Description;
namespace
WCF_NewFeatures
{
class
Program
{
static
void
Main(string
[]
args)
{
ServiceHost
serviceHost = new
ServiceHost
(typeof
(WCF_NewFeatures.EchoService
),new
Uri
("http://localhost:8080/Services/EchoService"
));
serviceHost.AddServiceEndpoint(typeof
(IEchoService
),new
WSHttpBinding
(),"newEndPoint"
);
serviceHost.Open();
System.Console
.WriteLine("The
EchoService has started"
);
foreach
(ServiceEndpoint
se in
serviceHost.Description.Endpoints)
{
System.Console
.WriteLine("Address:{0},
Binding:{1}, Contract:{2}"
, se.Address, se.Binding.Name,
se.Contract.Name);
}
System.Console
.WriteLine("Please,
press any key to finish ..."
);
System.Console
.ReadLine();
serviceHost.Close();
}
}
}
Listing 4
If you run the application, you will receive the output in the
Figure 2.
Figure 2
However you can also add the default endpoint along with your own
(see Figure 2).
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.ServiceModel;
using
System.ServiceModel.Description;
namespace
WCF_NewFeatures
{
class
Program
{
static
void
Main(string
[]
args)
{
ServiceHost
serviceHost = new
ServiceHost
(typeof
(WCF_NewFeatures.EchoService
),new
Uri
("http://localhost:8080/Services/EchoService"
));
serviceHost.AddServiceEndpoint(typeof
(IEchoService
),new
WSHttpBinding
(),"newEndPoint"
);
serviceHost.AddDefaultEndpoints();
serviceHost.Open();
System.Console
.WriteLine("The
EchoService has started"
);
foreach
(ServiceEndpoint
se in
serviceHost.Description.Endpoints)
{
System.Console
.WriteLine("Address:{0},
Binding:{1}, Contract:{2}"
, se.Address, se.Binding.Name,
se.Contract.Name);
}
System.Console
.WriteLine("Please,
press any key to finish ..."
);
System.Console
.ReadLine();
serviceHost.Close();
}
}
}
Listing 5
And the result is shown in the Figure 3.
Figure 3
For bindings the default is BasicHttpBinding. With WCF 4.0, you can
define default behavior configurations by omitting the name of the
configuration definition in the application configuration file (see
Listing 6).
<?
xml
version
=
"1.0
"
encoding
=
"utf-8
" ?>
<
configuration
>
<
system.serviceModel
>
<
behaviors
>
<
serviceBehaviors
>
<
behavior
name
=
"">
<
serviceMetadata
httpGetEnabled
=
"true
" />
</
behavior
>
</
serviceBehaviors
>
</
behaviors
>
</
system.serviceModel
>
</
configuration
>
Listing
6
In this case, we have turned on the service metadata, so you can
retrieve the service definition from the WSDL document (see Figure 4).
Figure 4
Standard Endpoint
Standard Endpoints are pre-configured endpoint definitions built
into the WCF 4.0 framework without the need to define them. These
endpoints are defined using the <Kind> attribute. The standard
endpoints are: mexEndpoint, dynamicEndpoint, discoveryEndpoint,
udpEndpoint, announcementEndpoint, udpAnnouncementEndpoint,
workflowControlEndpoint, webHttpEndpoint, webScriptEndpoint.
The mexEndpoint endpoint defines an endpoint for MEX configured
with IMetadataExchange for the service contract.
The dynamicEndpoint configures an endpoint with WS-Discovery
support within the a WCF client application.
The discoveryEndpoint configures the discovery operations within a
client application.
The udpDiscoveryEndpoint defines an endpoint for discovery
operations within a client application using UDP binding at a multicast
address.
The announcementEndpoint defines an endpoint for announcement
features of discovery.
The udpAnnouncementEndpoint defines an endpoint for announcement
features of discovery using the UDP protocol.
The workflowControlEndpoint defines an endpoint for controlling the
execution of workflow instances.
The webHttpEndpoint defines an endpoint using WebHttpBinding and
WebHttpBehavior to expose REST services.
The webScriptEndpoint defines an endpoint using WebHttpBinding and
WebHttpBehavior to expose Ajax services.
IIS hosting without a SVC file
In WCF 4.0, you can define virtual service activation endpoints in
the Web.config file order to activate a WCF service without the need of
.svc file (see Listing 7).
<
configuration
>
<
system.serviceModel
>
<
serviceHostingEnvironment
>
<
serviceActivations
>
<
add
relativeAddress
=
"EchoService.svc
"
service
=
"EchoService
"/>
</
serviceActivations
>
</
serviceHostingEnvironment
>
</
system.serviceModel
>
</
configuration
>
Listing 7
With this configuration, you can activate the EchoService using the
relative address EchoService.svc. If you create a Web application in
your IIS named EchoApp and copy the EchoService service definitions and
set up the service configuration as shown in the Listing 7, you can
browse to the service following the URI http://localhost/EchoApp/EchoService.svc
without having a physical EchoService.svc file with the service
activation definition.
Conclusion
分享到:
相关推荐
根据提供的文件信息,我们可以归纳出一系列与WCF 4.0相关的专业知识点。这份教材主要围绕Windows Communication Foundation (WCF) 4.0展开,详细介绍了如何利用.NET 4进行服务开发、部署和服务交互等内容。下面我们...
### WCF 4.0 多层服务开发与 LINQ to Entities #### 一、WCF 4.0 概述 Windows Communication Foundation (WCF) 是 Microsoft 提供的一个用于构建服务导向架构(SOA)应用程序的框架。WCF 4.0 作为 .NET Framework...
Packtpub WCF 4.0 Multi tier Services Development with LINQ to Entities Jun.2010 关于WCF 4.0 和LINQ to Entity 的 新书 流行加时尚的编程利器
在技术层面,本书涵盖了WCF 4.0的核心部分,包括服务模型、宿主模型、绑定、消息交换模式、元数据发布、安全性、事务处理、消息路由、可靠性以及宿主和部署等。这些内容帮助读者构建和维护WCF服务,以及了解如何在...
一个非常简单的WCF例子:Hello World 本解决方案有3个项目: 1、WCFClient:客户端,展示通过wcf后得到的结果,一个小的winform; 2、WCFContrlPanel:主控程序,作为打开/关闭wcf服务的控制面板,winform; 3、...
6. **服务质量**:WCF提供服务质量管理,包括服务质量QoS(Quality of Service),如消息队列、负载均衡和故障恢复,确保服务的高效稳定运行。 7. **服务发现**:WCF有内置的服务发现机制,使得服务可以在网络中被...
**创建一个简单的WCF程序:WcfServices与WcfServices2** Windows Communication Foundation(WCF)是.NET Framework中用于构建分布式应用程序的一种强大的服务导向架构。它允许开发人员创建跨平台的、安全的、可靠...
【标题】"我的第一个WCF程序:HelloInDigo"是一个初学者的实践项目,它展示了如何使用Windows Communication Foundation(WCF)技术创建一个简单的服务。WCF是.NET框架的一部分,用于构建分布式、面向服务的应用程序...
WCF全面解析:上册.part3.rar
标题中的“Packtpub.WCF.4.0.Multi.tier.Services.Development.with.LINQ.to.Entities.Jun.2010”暗示了这是一份关于使用Windows Communication Foundation(WCF)4.0开发多层服务的教程,其中特别强调了使用LINQ to...
Windows Communication Foundation (WCF) 是微软推出的一种用于构建分布式应用程序的框架,它集成了多种通信技术,为开发者提供了一种统一的方式来创建、发布和消费服务。WCF是.NET Framework 3.0及更高版本的一部分...
**WCF技术专题:WCF入门与进阶** Windows Communication Foundation(WCF)是微软推出的一种面向服务的架构,用于构建可互操作的分布式应用程序。它整合了.NET框架中的多种通信技术,如ASMX、Remoting、Web ...
WCF全面解析:上册.part2.rar
《Wrox Professional WCF 4: Windows Communication Foundation with .NET 4》这本书是关于Windows Communication Foundation(WCF)技术的权威指南,专为.NET Framework 4.0设计。WCF是微软提供的一种全面的、统一...