Add a new Visual Studio Team Test project called HostWCFServiceUnitTest from the Solution Explorer panel.. Add a service reference to the client test project HostWCFServiceUnitTest and e
Trang 1118
<remove name="WebServiceHandlerFactory-Integrated"/>
<remove name="ScriptHandlerFactory" />
<remove name="ScriptHandlerFactoryAppServices" />
<remove name="ScriptResource" />
<add name="ScriptHandlerFactory" verb="*" path="*.asmx"
preCondition="integratedMode"
type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> <add name="ScriptHandlerFactoryAppServices" verb="*" path="* AppService.axd" preCondition="integratedMode"
type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> <add name="ScriptResource" preCondition="integratedMode" verb="GET,HEAD"
path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral,
PublicKeyToken=31BF3856AD364E35" />
</handlers>
</system.webServer>
<system.serviceModel>
<client>
<remove contract="IMetadataExchange" name="sb" />
<endpoint address=""
binding="netTcpRelayBinding"
bindingConfiguration="metadataExchangeRelayBinding"
contract="IMetadataExchange" name="sb" />
</client>
<behaviors>
<serviceBehaviors>
<behavior name="HostWCFService_WebRole.UserRegisterServiceBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
<behavior name="HostWCFService_WebRole.Service1Behavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service
behaviorConfiguration="HostWCFService_WebRole.UserRegisterServiceBehavior" name="HostWCFService_WebRole.UserRegisterService">
<endpoint
address=""
binding="wsHttpBinding"
contract="AzureForDotNetDeveloperWCFServiceLibrary.IUserRegisterService"> <identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex"
Trang 2119
binding="mexHttpBinding"
contract="IMetadataExchange" />
</service>
<service behaviorConfiguration="HostWCFService_WebRole.Service1Behavior"
name="HostWCFService_WebRole.Service1">
<endpoint address=""
binding="wsHttpBinding"
contract="HostWCFService_WebRole.IService1">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange" />
</service>
</services>
</system.serviceModel>
</configuration>
5 Insert an App.config file to the project AzureForDotNetDeveloperWCFServiceLibrary
and modify it as shown in Listing 4-4
Listing 4-4 Configuration for AzureForDotNetDeveloperWCFServiceLibrary
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.web>
<compilation debug="true" />
</system.web>
<! When deploying the service library project, the content of the config
file must be added to the host's app.config file
System.Configuration does not support config files for libraries >
<system.serviceModel>
<services>
<service
behaviorConfiguration="AzureForDotNetDeveloperWCFServiceLibrary.Service1Behavior"
name="AzureForDotNetDeveloperWCFServiceLibrary.UserRegisterService">
<endpoint address="" binding="wsHttpBinding"
contract="AzureForDotNetDeveloperWCFServiceLibrary.IUserRegisterService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8080/UserRegisterService" />
</baseAddresses>
</host>
Trang 3120
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="AzureForDotNetDeveloperWCFServiceLibrary.Service1Behavior"> <! To avoid disclosing metadata information,
set the value below to false and remove the metadata endpoint above before deployment >
<serviceMetadata httpGetEnabled="True"/>
<! To receive exception details in faults for debugging purposes, set the value below to true Set to false before deployment
to avoid disclosing exception information >
<serviceDebug includeExceptionDetailInFaults="False" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
6 Add a new Visual Studio Team Test project called HostWCFServiceUnitTest from the Solution Explorer panel
7 Add a service reference to the client test project HostWCFServiceUnitTest and enter a name for that reference (the name can be changed after adding the reference) as shown in Figure 4-4
Figure 4-4 Add a service reference to the client application HostWCFServiceUnitTest
Trang 4121
8 Visual Studio will generate a service client proxy There is actually a package
generated, including service.wsdl, the web service definition language file,
and Reference.cs The first file contains information of the endpoint address,
service contract definition, and data object definitions The second one is
where the C# classes have been generated by Visual Studio using the WSDL
file Listing 5 is extracted from the generated service.wsdl file, and Listing
4-6 is the C# client proxy classes generated by Visual Studio If an application,
either a Windows application or a web application, is also developed using the
.NET platform, the generated C# proxy classes can be directly referenced An
application built with other platforms, such as Java, can use the generated
WSDL to create its own web service proxy and communicate with this service
The auto-generated proxy package should contain all service interface
functions as well as the data structure container classes To use these
generated classes from client applications should be straightforward as with
other regular C# classes
Listing 4-5 Extract from the Generated WSDL File service.wsdl
<wsdl:service name="UserRegisterService">
<wsdl:port name="WSHttpBinding IUserRegisterService"
binding="tns:WSHttpBinding IUserRegisterService">
<soap12:address location="http://localhost:8080/UserRegisterService" />
<wsa10:EndpointReference>
<wsa10:Address>http://localhost:8080/UserRegisterService</wsa10:Address>
<Identity xmlns="http://schemas.xmlsoap.org/ws/2006/02/addressingidentity">
<Dns>localhost</Dns>
</Identity>
</wsa10:EndpointReference>
</wsdl:port>
</wsdl:service>
Listing 4-6 Service Proxy Classes Generated by Visual Studio
// -
// <auto-generated>
// This code was generated by a tool
// Runtime Version:2.0.50727.3053
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated
// </auto-generated>
// -
namespace HostWCFSrviceUnitTest.HostWCFService {
using System.Runtime.Serialization;
using System;
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization",
Trang 5122
"3.0.0.0")]
[System.Runtime.Serialization.DataContractAttribute(
Name="User",
Namespace=
"http://schemas.datacontract.org/2004/07/AzureForDotNetDeveloperWCFServiceLibrary")]
[System.SerializableAttribute()]
public partial class User : object, System.Runtime.Serialization.IExtensibleDataObject, System.ComponentModel.INotifyPropertyChanged {
[System.NonSerializedAttribute()]
private System.Runtime.Serialization.ExtensionDataObject extensionDataField;
[System.Runtime.Serialization.OptionalFieldAttribute()]
private string FirstNameField;
[System.Runtime.Serialization.OptionalFieldAttribute()]
private string LastNameField;
[System.Runtime.Serialization.OptionalFieldAttribute()]
private string PasswordField;
[System.Runtime.Serialization.OptionalFieldAttribute()]
private System.DateTime TimeRegisteredField;
[global::System.ComponentModel.BrowsableAttribute(false)]
public System.Runtime.Serialization.ExtensionDataObject ExtensionData {
get {
return this.extensionDataField;
}
set {
this.extensionDataField = value;
}
}
[System.Runtime.Serialization.DataMemberAttribute()]
public string FirstName {
get {
return this.FirstNameField;
}
set {
if ((object.ReferenceEquals(this.FirstNameField, value) != true)) {
this.FirstNameField = value;
this.RaisePropertyChanged("FirstName");
}
}
}
[System.Runtime.Serialization.DataMemberAttribute()]
public string LastName {
get {
return this.LastNameField;