C# 我如何使用 WCF 数据服务?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15254251/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do i Consume WCF Data Service?
提问by Idrees Khan
i have created a wcf service but i have used 3 projects for it;
1) ServiceLibrary (WCF library)
2) Web
3) ConsoleTestClient
my ServiceLibrary
app.config file looks like this;
我创建了一个 wcf 服务,但我使用了 3 个项目;
1) ServiceLibrary (WCF 库)
2) Web
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 />
Now, to host this library, i have done the following settings in my Web.Config
file of the Web
Project.
The svc file name is WcfDataService1.svc
现在,为了托管这个库,我在我Web.Config
的Web
项目文件中进行了以下设置。
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>
Now, when i test this service using directly (ServiceLibrary project) using WCF test client, i see the following and works everything great;
The issue is when i try to run my Web
project(which i use as a host for wcf service). And then go to the console test client and want to add reference using add reference. I don't see my Get
and Set
methods (like test client)
Why i don't see my IAdvertisementService interface and the methods
Do i have to deploy this to actuall IIS?
现在,当我使用 WCF 测试客户端直接使用(ServiceLibrary 项目)测试此服务时,我看到以下内容并且一切正常;
问题是当我尝试运行我的Web
项目(我用作 wcf 服务的主机)时。然后转到控制台测试客户端并希望使用添加引用添加引用。我没有看到我的Get
和Set
方法(如测试客户端)
为什么我没有看到我的 IAdvertisementService 接口和方法
我是否必须将它部署到实际的 IIS?
回答by Greg
Previous Post Removed:
以前的帖子已删除:
Update:
更新:
The Microsoft Developer Network actually covers this in great detail, some of the resources they provide are:
Microsoft Developer Network 实际上非常详细地介绍了这一点,他们提供的一些资源是:
There are also several books that my solve this particular endeavor. Since someone stated that providing links to solve this issue doesn't truly answer your question I'll attempt to.
还有几本书可以帮助我解决这个特殊的问题。由于有人表示提供解决此问题的链接并不能真正回答您的问题,因此我会尝试回答。
- Inside Visual Studio click File, then proceed to New Project.
- In the dialog expand Visual C#, Select Weband ASP.NET Web Forms Application.
- Give your project a name of your choice
NorthwindWeb
.
- 在 Visual Studio 中点击File,然后进入New Project。
- 在对话框中展开Visual C#,选择Web和ASP.NET Web 窗体应用程序。
- 为您的项目取一个您选择的名称
NorthwindWeb
。
At this point you've created a project; due to the complexity of a Serviceoverlooking a tiny detail can be catastrophic in the outcome. That is why I'm starting from scratch.
此时您已经创建了一个项目;由于服务的复杂性,忽略一个微小的细节可能会给结果带来灾难性的后果。这就是为什么我要从头开始。
In my example I'll link it to a Database. So I'll add a Ado.Net Entity Data Model. I'll name my model: NorthwindModel
. I'm also going to generate based upon an existing Database. So up at this point just follow the Visual Studio Wizard. Choose your Database Objectswithin those tables, then Finish.
在我的示例中,我将它链接到一个Database。所以我将添加一个Ado.Net 实体数据模型。我将命名我的模型:NorthwindModel
. 我还将根据现有的Database生成。因此,此时只需按照 Visual Studio 向导进行操作即可。在这些表中选择您的数据库对象,然后完成。
The important part, building my Data Service
.
重要的部分,构建我的Data Service
.
- Project Add New Item.
- Select Weband choose WCF Data Service.
- Put a name,
NorthwindCustomer
- Then Add.
- 项目添加新项目。
- 选择Web并选择WCF 数据服务。
- 输入名称,
NorthwindCustomer
- 然后添加。
Locate the first Todo:
Comment and remove the code then put:
找到第一个Todo:
Comment 并删除代码然后输入:
public class DemonDbCustomer : DataService<demonDbEntities>
Then find the comments in the InitializeService
Event Handler:
然后在InitializeService
Event Handler中找到注释:
config.SetEntitySetAccessRule("*", EntitySetRights.All);
At this point hit CTRL + F5to run the service. The browser will open and the XML Schema for the service will generate. In the Address Bartype in Customersat the end of the URL for NorthwindCustomers.svc
and hit Enter.
此时按CTRL + F5运行服务。浏览器将打开并生成服务的 XML 架构。在地址栏中,在 URL 末尾的客户中键入NorthwindCustomers.svc
并按Enter 键。
** Sometimes Internet Explorer will mess this up, so additional troubleshooting may be required. **
** 有时 Internet Explorer 会将其搞砸,因此可能需要进行额外的故障排除。**
Now we will create our Client Portion.
现在我们将创建我们的客户部分。
- Adda New Project
- Select Windows Forms Application
- Name the file of your choice,
NorthwindClient
then click Ok. - In the Solution Explorerselect the
NorthwindClient Project
and Set As Startup Project. - Right Click on Project: Add Service ReferenceClick Discover.
- 添加一个新项目
- 选择Windows 窗体应用程序
- 命名您选择的文件,
NorthwindClient
然后单击Ok。 - 在解决方案资源管理器中选择
NorthwindClient Project
并设置为启动项目。 - 右键单击项目:添加服务引用单击发现。
At this point your URL for NorthwindCustomers Service
will appear in that Addressfield. This is generated from that .svc
file.
此时,您的 URLNorthwindCustomers Service
将出现在该地址字段中。这是从该.svc
文件生成的。
Now we have to provide data binding to our service.
现在我们必须为我们的服务提供数据绑定。
- On the Data Menuwe want to Show Data Sources.
- Add New Data Source
- Choose the type of
Data Source
and follow the Wizard (Click Object). - Select the Object you wish to bind.
- 在数据菜单上,我们要显示数据源。
- 添加新数据源
- 选择类型
Data Source
并按照向导操作(单击对象)。 - 选择要绑定的对象。
Now at this point you just need to create a User Interface
. To do so just simply drag your Customers Node
from your Data Sources
to the Form
.
现在,您只需要创建一个User Interface
. 为此,只需简单地将您的Customers Node
从您Data Sources
的Form
.
DataGridView
BindingSource
BindingNavigation
DataGridView
BindingSource
BindingNavigation
They are all added automatically. Then simply double click your Form
and add the following to your Form1_Load Event Handler
.
它们都是自动添加的。然后只需双击您的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;
Now in your Solution Explorerright click that NorthwindCustomers.svc
and click View In Browser. The XML Schema will be added, so you just copy that URL from the Address Bar. Then replace the Uri
with the one you just copied.
现在在您的解决方案资源管理器中右键单击它NorthwindCustomers.svc
并单击View In Browser。将添加 XML 架构,因此您只需从Address Bar复制该 URL 。然后用Uri
刚才复制的那个替换。
Run your application and you've down the following:
运行您的应用程序,您已完成以下操作:
- Host
- Client
- Service
- Consumed
- 主持人
- 客户
- 服务
- 消耗
That is how to consume a WCF Data Servicethe article that has even more detail is here:
这就是如何使用WCF 数据服务,这里有更详细的文章:
Hopefully that helps.
希望这有帮助。
回答by Saddam Mohsen
To develop a service using ASP.NET, we must add the WebService attribute to the class and WebMethodAttribute to any of the class methods.
要使用 ASP.NET 开发服务,我们必须将 WebService 属性添加到类,并将 WebMethodAttribute 添加到任何类方法。
Example
例子
[WebService]
public class Service : System.Web.Services.WebService
{
[WebMethod]
public string Test(string strMsg)
{
return strMsg;
}
}
To develop a service in WCF, we will write the following code:
要在 WCF 中开发服务,我们将编写以下代码:
[ServiceContract]
public interface ITest
{
[OperationContract]
string ShowMessage(string strMsg);
}
public class Service : ITest
{
public string ShowMessage(string strMsg)
{
return strMsg;
}
}
The ServiceContractAttribute specifies that an interface defines a WCF service contract, OperationContract attribute indicates which of the methods of the interface defines the operations of the service contract.
ServiceContractAttribute 指定接口定义 WCF 服务契约,OperationContract 属性指示接口的哪些方法定义了服务契约的操作。
A class that implements the service contract is referred to as a service type in WCF.
实现服务契约的类在 WCF 中称为服务类型。
Hosting the Service
托管服务
ASP.NET web services are compiled into a class library assembly and a service file with an extension .asmx will have the code for the service. The service file is copied into the root of the ASP.NET application and Assembly will be copied to the bin directory. The application is accessible using URL of the service file.
ASP.NET Web 服务被编译为类库程序集,扩展名为 .asmx 的服务文件将包含该服务的代码。服务文件被复制到 ASP.NET 应用程序的根目录中,程序集将被复制到 bin 目录中。可以使用服务文件的 URL 访问该应用程序。
WCF Service can be hosted within IIS or WindowsActivationService.
WCF 服务可以托管在 IIS 或 WindowsActivationService 中。
Compile the service type into a class library Copy the service file with an extension .SVC into a virtual directory and assembly into bin sub directory of the virtual directory. Copy the web.config file into the virtual directory.
将服务类型编译成类库将扩展名为.SVC的服务文件复制到虚拟目录下,汇编到虚拟目录下的bin子目录下。将 web.config 文件复制到虚拟目录中。
Client Development
客户开发
Clients for the ASP.NET Web services are generated using the command-line tool WSDL.EXE.
ASP.NET Web 服务的客户端是使用命令行工具 WSDL.EXE 生成的。
WCF uses the ServiceMetadata tool (svcutil.exe) to generate the client for the service.
WCF 使用 ServiceMetadata 工具 (svcutil.exe) 为服务生成客户端。
For more detail goto this link http://www.codeproject.com/Articles/139787/What-s-the-Difference-between-WCF-and-Web-Services
有关更多详细信息,请转到此链接 http://www.codeproject.com/Articles/139787/What-s-the-Difference-between-WCF-and-Web-Services