[英]How do i Consume WCF Data Service?
我已經創建了一個wcf服務,但我已經使用了3個項目;
1)ServiceLibrary(WCF庫)
2)網絡
3)ConsoleTestClient
我的ServiceLibrary
app.config文件看起來像這樣;
<system.serviceModel>
<services>
<service name="MrDAStoreJobs.ServiceLibrary.AdvertisementService">
<clear />
<endpoint address="basic"
binding="basicHttpBinding" bindingConfiguration=""
contract="MrDAStoreJobs.ServiceLibrary.Interface.IAdvertisementService" />
<endpoint name="mexHttpBinding"
contract="IMetadataExchange"
binding="mexHttpBinding"
address="mex" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:13758/" />
</baseAddresses>
</host>
</service>
</services>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information,
set the value below to false before deployment -->
<serviceMetadata httpGetEnabled="True" />
<!-- To receive exception details in faults for debugging purposes,
set the value below to true. Set to false before deployment
to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel> <br />
現在,為了托管這個庫,我在Web
Project的Web.Config
文件中完成了以下設置。
svc文件名是WcfDataService1.svc
public class WcfDataService1 : DataService<AdvertisementService>
{
// This method is called only once to initialize service-wide policies.
public static void InitializeService(DataServiceConfiguration config)
{
config.UseVerboseErrors = true;
ServiceOperationRights.All);
config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V3;
}
}
<system.serviceModel>
<services>
<service name="MrDAStoreJobs.ServiceLibrary.AdvertisementService">
<clear />
<endpoint address="basic"
binding="basicHttpBinding" bindingConfiguration=""
contract="MrDAStoreJobs.ServiceLibrary.Interface.IAdvertisementService" />
<endpoint name="mexHttpBinding"
contract="IMetadataExchange"
binding="mexHttpBinding"
address="mex" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:13758/WcfDataService1.svc" />
</baseAddresses>
</host>
</service>
</services>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information,
set the value below to false before deployment -->
<serviceMetadata httpGetEnabled="True" />
<!-- To receive exception details in faults for debugging purposes,
set the value below to true. Set to false before deployment
to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
現在,當我使用WCF測試客戶端直接使用(ServiceLibrary項目)測試此服務時,我看到以下內容並且一切都很好;
問題是當我嘗試運行我的Web
項目(我用作wcf服務的主機)。 然后轉到控制台測試客戶端,並希望使用添加引用添加引用。 我沒有看到我的Get
和Set
方法(比如測試客戶端) 為什么我沒有看到我的IAdvertisementService接口和方法
我是否必須將其部署到執行IIS?
若要使用ASP.NET開發服務,我們必須將WebService屬性添加到類中,並將WebMethodAttribute添加到任何類方法中。
例
[WebService]
public class Service : System.Web.Services.WebService
{
[WebMethod]
public string Test(string strMsg)
{
return strMsg;
}
}
要在WCF中開發服務,我們將編寫以下代碼:
[ServiceContract]
public interface ITest
{
[OperationContract]
string ShowMessage(string strMsg);
}
public class Service : ITest
{
public string ShowMessage(string strMsg)
{
return strMsg;
}
}
ServiceContractAttribute指定接口定義WCF服務合同,OperationContract屬性指示接口的哪些方法定義服務合同的操作。
實現服務合同的類在WCF中稱為服務類型。
托管服務
ASP.NET Web服務被編譯為類庫程序集,擴展名為.asmx的服務文件將具有該服務的代碼。 該服務文件將被復制到ASP.NET應用程序的根目錄中,而Assembly將被復制到bin目錄中。 可以使用服務文件的URL訪問該應用程序。
WCF服務可以在IIS或WindowsActivationService中托管。
將服務類型編譯到類庫中將擴展名為.SVC的服務文件復制到虛擬目錄中,然后將其匯編到虛擬目錄的bin子目錄中。 將web.config文件復制到虛擬目錄中。
客戶開發
使用命令行工具WSDL.EXE生成ASP.NET Web服務的客戶端。
WCF使用ServiceMetadata工具(svcutil.exe)生成服務的客戶端。
有關更多詳細信息, 請訪問此鏈接http://www.codeproject.com/Articles/139787/What-s-the-Difference-between-WCF-and-Web-Services
上一篇文章刪除:
更新:
Microsoft開發人員網絡實際上涵蓋了這一點,它們提供的一些資源包括:
還有幾本書我解決了這個特別的努力。 由於有人說提供解決此問題的鏈接並不能真正回答您將要嘗試解決的問題。
NorthwindWeb
。 至此,您已經創建了一個項目。 由於服務的復雜性,忽略微小的細節可能會導致災難性的后果。 這就是為什么我從頭開始。
在我的示例中,我將其鏈接到數據庫 。 所以我將添加一個Ado.Net實體數據模型 。 我將命名我的模型: NorthwindModel
。 我還將基於現有數據庫進行生成。 所以在這一點上只需遵循Visual Studio向導。 在這些表中選擇數據庫對象 ,然后單擊完成。
重要的部分,構建我的Data Service
。
NorthwindCustomer
- 然后添加 。 找到第一個Todo:
注釋並刪除代碼然后放入:
public class DemonDbCustomer : DataService<demonDbEntities>
然后在InitializeService
事件處理程序中找到注釋:
config.SetEntitySetAccessRule("*", EntitySetRights.All);
此時按CTRL + F5運行該服務。 瀏覽器將打開,並且將生成該服務的XML模式。 在地址欄中 ,在NorthwindCustomers.svc
的URL末尾鍵入客戶 ,然后按Enter 。
**有時Internet Explorer會將其弄亂,因此可能需要進行其他故障排除。 **
現在,我們將創建客戶部分。
NorthwindClient
然后單擊確定 。 NorthwindClient Project
和Set As Startup Project 。 此時,您NorthwindCustomers Service
的URL將顯示在該“ 地址”字段中。 這是從.svc
文件生成的。
現在我們必須提供數據綁定到我們的服務。
Data Source
的類型,然后按照向導(單擊對象)。 現在,此時您只需要創建一個User Interface
。 為此,只需將“ Customers Node
從Data Sources
拖動到Form
。
DataGridView
BindingSource
BindingNavigation
它們都是自動添加的。 然后只需雙擊您的Form
並將以下內容添加到Form1_Load Event Handler
。
ServiceReference1.northwindModel.northwindEntities proxy = new
ServiceReference1.northwindModel.northwindEntities(new
Uri("http://localhost:53397/NorthwindCustomers.svc/"));
// As you see it pointed to our SVC file, because that includes our Address, Binding, Contract information.
this.customersBindingSource.DataSource = proxy.Customers;
現在,在解決方案資源管理器中,右鍵單擊NorthwindCustomers.svc
,然后單擊“在瀏覽器中查看” 。 將添加XML模式,因此您只需從地址欄中復制該URL。 然后用您剛復制的Uri
替換Uri
。
運行您的應用程序,您已經完成以下操作:
希望這會有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.