如何确定WCF数据服务的客户端引用的URL?

时间:2011-10-13 03:21:10

标签: c# windows-phone-7 wcf-data-services

要在Windows Phone上访问OData,请执行以下操作:

// Declare the data service objects and URIs.
NorthwindEntities context;
Uri northwindUri =
    new Uri("http://services.odata.org/Northwind/Northwind.svc/");
DataServiceCollection<Customer> customers;

// Initialize the context and the binding collection 
context = new NorthwindEntities(northwindUri);
customers = new DataServiceCollection<Customer>(context);

// Define a LINQ query that returns all customers.
var query = from cust in context.Customers
            select cust;

// Register for the LoadCompleted event.
customers.LoadCompleted
    += new EventHandler<LoadCompletedEventArgs>(customers_LoadCompleted);

// Load the customers feed by executing the LINQ query.
customers.LoadAsync(query);

但我已经知道服务参考中的URL。

我不能把它传递给URI参数吗?

是否有一种简单的方法可以访问其配置的网址?

这是个好主意吗?

1 个答案:

答案 0 :(得分:1)

如果您定义了服务客户端,则可以通过执行以下操作获取URI:

client.Endpoint.Address.Uri

在app.config中定义了服务引用:

<client>
  <endpoint address="http://localhost:36294/Services/Service1.svc"
      binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IService1"
      contract="ServiceReference1.IService1" name="BasicHttpBinding_IService1" />
</client>

如果您的应用程序中已有此功能(web应用程序的web.config)。然后你根本不需要定义端点,因为它已经存在并且将在实例化时被抓取。

假设该服务是WCF服务,并通过“添加服务引用...”添加引用