如何正确对Silverlight WCF创建("Silverlight WCF服务创建指南:正确步骤详解")
原创
一、引言
在Silverlight应用程序中,我们常常需要与后端服务进行通信。WCF(Windows Communication Foundation)是一个有力的框架,用于构建服务导向的应用程序。本文将详细介绍怎样在Silverlight中创建并使用WCF服务。
二、环境准备
在开端之前,请确保您的开发环境已经安装了以下软件:
- Visual Studio 2010 或更高版本
- .NET Framework 3.5 或更高版本
- SQL Server 2008 或更高版本(如果需要数据库赞成)
三、创建WCF服务项目
以下是创建WCF服务的步骤:
1. 创建一个新的WCF服务项目
在Visual Studio中,选择“文件”->“新建”->“项目”。在弹出的“新建项目”对话框中,选择“WCF”->“WCF服务应用程序”,并为项目命名。
2. 添加服务契约
在项目中,添加一个新的类文件,并定义服务契约。例如,创建一个名为“IWeatherService.cs”的文件,并添加以下代码:
using System.ServiceModel;
[ServiceContract]
public interface IWeatherService
{
[OperationContract]
string GetWeather(string city);
}
3. 实现服务
在项目中添加一个新的类文件,实现服务契约。例如,创建一个名为“WeatherService.cs”的文件,并添加以下代码:
using System.ServiceModel;
public class WeatherService : IWeatherService
{
public string GetWeather(string city)
{
switch (city)
{
case "北京":
return "晴天";
case "上海":
return "多云";
case "广州":
return "雨天";
default:
return "未知";
}
}
}
4. 配置服务终结点
在项目中的“App.config”文件中,配置服务终结点。以下是一个示例配置:
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="wsHttpBinding" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="8192"
maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<reliableSession ordered="true" inactivityTimeout="00:10:00"
enableSession="true" />
</binding>
</wsHttpBinding>
</bindings>
<services>
<service name="WeatherService" behaviorConfiguration="ServiceBehavior">
<endpoint address="" binding="wsHttpBinding" contract="IWeatherService" />
<endpoint address="mex" binding="mexHttpsBinding" contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior">
<serviceMetadata httpsGetEnabled="true" />
<serviceCredentials>
<serviceCertificate findValue="localhost" storeLocation="LocalMachine"
storeName="My" x509FindType="FindBySubjectName" />
</serviceCredentials>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
四、在Silverlight应用程序中调用WCF服务
在Silverlight应用程序中调用WCF服务,需要进行以下步骤:
1. 添加服务引用
在Silverlight项目中,右键点击“引用”->“添加服务引用”。在弹出的“添加服务引用”对话框中,输入WCF服务的地址,然后点击“前往”。选择要引用的服务,并为其命名。
2. 调用服务
在Silverlight项目中,使用添加的服务引用调用WCF服务。以下是一个示例代码:
WeatherServiceClient client = new WeatherServiceClient();
client.GetWeatherCompleted += new EventHandler<GetWeatherCompletedEventArgs>(client_GetWeatherCompleted);
client.GetWeatherAsync("北京");
void client_GetWeatherCompleted(object sender, GetWeatherCompletedEventArgs e)
{
MessageBox.Show(e.Result);
}
五、常见问题与解决方法
以下是创建和调用Silverlight WCF服务过程中或许遇到的一些常见问题及其解决方法:
1. 无法访问WCF服务
如果无法访问WCF服务,请检查以下方面:
- 确保WCF服务已启动并运行。
- 检查服务地址是否正确。
- 确保网络连接正常。
- 检查防火墙设置,确保端口未被封锁。
2. 服务调用超时
如果服务调用超时,请检查以下方面:
- 检查网络连接是否正常。
- 提高服务调用的超时时间。
- 检查服务端是否存在性能问题。
六、总结
本文详细介绍了怎样在Silverlight应用程序中创建和调用WCF服务。通过遵循上述步骤,您应该能够胜利地创建并使用WCF服务。请注意,在实际项目中,您或许需要结合具体需求调整服务配置和代码实现。