Azure開發者任務之八:使用WCF Service Web Role
原創在本文中,我們將會在local development fabric上創建一個WCF服務角色,然后在一個控制臺應用程序中使用它。
WCF服務角色可以讓我們創建一個WCF服務,并且把它托管在Windows Azure中。在本文中,我們將會創建一個WCF服務角色,把它托管在local development fabric上,然后在一個控制臺應用程序中使用它。在下一篇文章中,我們將會把這個WCF服務遷移到Azure門戶中。
首先
1,創建一個新項目
2,導航到“Cloud”標簽
3,創建Windows Azure項目
4,在給出的選項中選擇WCF服務角色
如果你看一下解決方案資源管理器,你會發現這個WCF服務角色項目的結構和文件與正常創建的WCF服務應用程序完全相同。它包含:
1,IService1.cs(服務契約)
2,Service1.svc.cs(服務定義)
3,Web.config(EndPoint的配置)
我們可以按照我們的需求來修改這些文件,這和我們平時在一個WCF服務應用程序中做的事情沒有什么兩樣。
讓我們來修改服務契約:
IService1.svc
using System.ServiceModel;
namespace WCFServiceWebRole1
{
[ServiceContract]
public interface IService1
{
[OperationContract]
string GetData(int value);
}
}
然后,修改服務定義:
Service1.svc.cs
namespace WCFServiceWebRole1
{
public class Service1 : IService1
{
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
}
}
在Web.Config中保留默認的配置。
確保你已經把Windows Azure項目設置成啟動項目了,然后運行這個應用程序。在瀏覽器中,你會得到如下錯誤信息:
忽略這個錯誤信息,然后把一個URL添加到“Service1.svc”中,這個URL應該是http://127.0.0.1:81/Service1.svc。“Service1.svc”是服務定義的名字。添加以后,你會在瀏覽器中得到通常的WCF服務消息
要在一個控制臺客戶端中測試這個WCF服務角色,需要:
1,創建一個控制臺應用程序項目
2,使用指定的URL(http://127.0.0.1:81/Service1.svc)來添加服務引用
現在,我們編寫一個正常的服務調用:
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ConsoleApplication14.ServiceReference1;
namespace ConsoleApplication14
{
class Program
{
static void Main(string[] args)
{
Service1Client proxy = new Service1Client();
var result = proxy.GetData(99);
Console.WriteLine(result);
Console.ReadKey(true);
}
}
}
現在,當你運行這個應用程序的時候,你也許會得到下面這個異常:
要解決上面這個異常,我們必須要編輯“App.Config”文件。我們需要修改這個配置文件:
“127.0.0.1”代表“localhost”。也許這個控制臺應用程序不能解析“127.0.0.1”,所以,我們把它改成“localhost”:
現在,運行這個應用程序,我們會得到如下輸出:
這里,我們需要注意的一個行為是,有時,在把“127.0.0.1”改成“localhost”以后,你可能還是會得到那個超時異常。在我以后的文章中,我會深入研究一下這個意料之外的行為。
原文名:Windows Azure for Developers Task 8: Working with WCF Service Web Role 作者: Dhananjay Kumar
【本文乃51CTO精選譯文,轉載請標明出處!】
【編輯推薦】
- 微軟公布云計算平臺Azure收費模式細節
- 云計算意在長遠,微軟云計算服務Windows Azure已經啟用
- 技術透析:Windows Azure Platform框架與組成
- 微軟Windows Azure Platform技術解析
- 走近微軟云:SQL Server到Azure數據同步
- 當微軟Azure遭遇亞馬遜EC2:五大關鍵區別
- Windows Azure云計算平臺新增五大功能
- 云計算前途光明 Azure用戶數突破31000
- 如何把應用程序部署到Windows Azure中