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

本文关键字:引用 URL 客户端 服务 何确定 WCF 数据 | 更新日期: 2023-09-27 18:11:12

要访问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参数吗?

是否有一个简单的方法来访问它的配置URL?

这是个好主意吗?

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

如果您定义了一个服务客户端,您可以通过以下操作获得使用的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应用程序)。那么你根本不需要定义端点,因为它已经存在,并且会在实例化时被抓取。

这是假设服务是一个WCF服务,并且引用是通过'Add service reference…'添加的