namespaceProductsServer
{
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.ServiceModel;
// Define the data contract for the service
[DataContract]
// Declare the serializable properties.publicclassProductData
{
[DataMember]
publicstring Id { get; set; }
[DataMember]
publicstring Name { get; set; }
[DataMember]
publicstring Quantity { get; set; }
}
// Define the service contract.
[ServiceContract]
interfaceIProducts
{
[OperationContract]
IList<ProductData> GetProducts();
}
interfaceIProductsChannel : IProducts, IClientChannel
{
}
}
在 Program.cs 中,将命名空间定义替换为以下代码,以便为其添加配置文件服务和主机。
C#
namespaceProductsServer
{
using System;
using System.Linq;
using System.Collections.Generic;
using System.ServiceModel;
// Implement the IProducts interface.classProductsService : IProducts
{
// Populate array of products for display on website
ProductData[] products =
new []
{
new ProductData{ Id = "1", Name = "Rock",
Quantity = "1"},
new ProductData{ Id = "2", Name = "Paper",
Quantity = "3"},
new ProductData{ Id = "3", Name = "Scissors",
Quantity = "5"},
new ProductData{ Id = "4", Name = "Well",
Quantity = "2500"},
};
// Display a message in the service console application// when the list of products is retrieved.public IList<ProductData> GetProducts()
{
Console.WriteLine("GetProducts called.");
return products;
}
}
classProgram
{
// Define the Main() function in the service application.staticvoidMain(string[] args)
{
var sh = new ServiceHost(typeof(ProductsService));
sh.Open();
Console.WriteLine("Press ENTER to close");
Console.ReadLine();
sh.Close();
}
}
}
在“解决方案资源管理器”中,双击“App.config”在 Visual Studio 编辑器中将其打开。 在 <system.ServiceModel> 元素的底部(但仍在 <system.ServiceModel> 中)添加以下 XML 代码。
重要
将 yourServiceNamespace 替换为你的命名空间名称,并将 yourKey 替换为前面从门户中检索到的 SAS 密钥:
<appSettings><!-- Service Bus specific app settings for messaging connections --><addkey="Microsoft.ServiceBus.ConnectionString"value="Endpoint=sb://yourNamespace.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=yourKey"/></appSettings>
在“创建新项目”中,选择适用于 C# 的“ASP.NET Web 应用程序(.NET Framework)”,然后选择“下一步” 。
将项目命名为 ProductsPortal,然后选择“创建”。
在“创建新 ASP.NET Web 应用程序”中选择“**MVC”,然后选择“身份验证”下的“更改”。
在“更改身份验证”中,依次选择“无身份验证”、“确定”。 在本教程中,你将部署无需用户登录的应用。
返回“创建新 ASP.NET Web 应用程序”,选择“创建”以创建 MVC 应用。
配置新 Web 应用的 Azure 资源。 遵循发布 Web 应用中的步骤。 然后,返回本教程并继续执行下一步。
在“解决方案资源管理器”中,右键单击“模型”并选择“添加”>“类”。
将类命名为 Product.cs,然后选择“添加”。
修改 Web 应用程序
在 Visual Studio 的 Product.cs 文件中将现有命名空间定义替换为以下代码:
C#
// Declare properties for the products inventory.namespaceProductsWeb.Models
{
publicclassProduct
{
publicstring Id { get; set; }
publicstring Name { get; set; }
publicstring Quantity { get; set; }
}
}
在“解决方案资源管理器”中展开“控制器”,然后双击“HomeController.cs”在 Visual Studio 中打开该文件。
在 HomeController.cs中,将现有命名空间定义替换为以下代码:
C#
namespaceProductsWeb.Controllers
{
using System.Collections.Generic;
using System.Web.Mvc;
using Models;
publicclassHomeController : Controller
{
// Return a view of the products inventory.public ActionResult Index(string Identifier, string ProductName)
{
var products = new List<Product>
{new Product {Id = Identifier, Name = ProductName}};
return View(products);
}
}
}
在“解决方案资源管理器”中展开“视图”>“共享”,然后双击“_Layout.cshtml”在 Visual Studio 编辑器中打开该文件。
将出现的所有 My ASP.NET Application 更改为“Northwind Traders 产品”。
删除 Home、About 和 Contact 链接。 在以下示例中,删除突出显示的代码。
在“解决方案资源管理器”中展开“视图”>“主目录”,然后双击“Index.cshtml”在 Visual Studio 编辑器中打开该文件。 将文件的全部内容替换为以下代码: