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
Azure开发者任务之六:使用WCF Service Web Role的更多相关文章
- Azure开发者任务之七:在Azure托管服务中托管WCF服务角色
在一个托管服务中托管一个WCF服务角色和托管一个ASP.Net Web Role基本类似. 在上一篇文章中,我们学习了如何使用WCF Service Web Role. 在本文中,我会对上一篇文章进行 ...
- Azure开发者任务之三:理解Azure应用程序(上)
作为Windows Azure的托管服务被设计和开发的应用程序由这两部分组成: 1,托管代码 2,XML配置文件 托管代码对应不同的角色 XML文件对应不同的配置设置 我们可以看一下下面这张图,它详细 ...
- [Windows Azure] Building the web role for the Windows Azure Email Service application - 3 of 5
Building the web role for the Windows Azure Email Service application - 3 of 5. This is the third tu ...
- Azure开发者任务之二:Cloud Service项目添加到ASP.Net Web中
假设我们正在把现有的Web应用程序或ASP.Net MVC Web应用程序迁移到云中.在这种情况下,我们需要把云服务添加到现有的Web应用程序或ASP.Net MVC Web应用程序中. 我们有一个W ...
- wcf和web service的区别
1.WebService:严格来说是行业标准,不是技术,使用XML扩展标记语言来表示数据(这个是夸语言和平台的关键).微软的Web服务实现称为ASP.NET Web Service.它使用Soap简单 ...
- WCF和Web Service的 区(guan)别(xi)
参考文献:http://social.microsoft.com/Forums/zh-CN/c06420d1-69ba-4aa6-abe5-242e3213b68f/wcf-webservice 之前 ...
- WCF、Web API、WCF REST、Web Service比较
原文地址:http://www.dotnet-tricks.com/Tutorial/webapi/JI2X050413-Difference-between-WCF-and-Web-API-and- ...
- Difference between WCF and Web API and WCF REST and Web Service
The .Net framework has a number of technologies that allow you to create HTTP services such as Web S ...
- WCF 、Web API 、 WCF REST 和 Web Service 的区别
WCF .Web API . WCF REST 和 Web Service 的区别 The .Net framework has a number of technologies that allow ...
随机推荐
- QQ空间直播秒开优化实践[读]
http://mp.weixin.qq.com/s?__biz=MzI1MTA1MzM2Nw==&mid=2649796799&idx=1&sn=42061b7d021b8d8 ...
- Entity Framework 在Vs2012下Update Model From DataBase 失败的问题
http://stackoverflow.com/questions/13054212/vs-2012-ef-5-0-update-model-from-database-not-picking-up ...
- egit - not authorized
A. To specify credentials individually for each remote Open Git repositories view, open "Remote ...
- iOS开发——项目实战总结&经典错误一
经典错误一 No architectures to compile for (ONLY_ACTIVE_ARCH=YES, active arch=armv7, VA 运行报错 出现的原因:armv7s ...
- Spring3系列12- Spring AOP AspectJ
Spring3系列12- Spring AOP AspectJ 本文讲述使用AspectJ框架实现Spring AOP. 再重复一下Spring AOP中的三个概念, Advice:向程序内部注入的代 ...
- nginx 相关问题
Nginx配置文件nginx.conf 参考:http://www.2cto.com/os/201212/176520.html Nginx自动切分日志: nignx没有自动分开文件存储日志的机制. ...
- Ranorex入门指南
Ranorex入门指南 http://automationqa.com/forum.php?mod=viewthread&tid=2766&fromuid=29
- Python strange questions list
sys.setrecursionlimit(1<<64) Line 3: OverflowError: Python int too large to convert to C long ...
- 何为.Net Remoting【转】
借助基维百科给它的定义如下: NET Remoting 是微软 .NET Framework 中的一种网络通讯技术,与 XML Web Service 不同的是,它可以使用 SOAP 以外的协定来通讯 ...
- 用 Python 和 OpenCV 检测图片上的条形码
用 Python 和 OpenCV 检测图片上的的条形码 这篇博文的目的是应用计算机视觉和图像处理技术,展示一个条形码检测的基本实现.我所实现的算法本质上基于StackOverflow 上的这个问 ...