我如何使用WCF数据服务?
我已创建的WCF服务,但我已经使用3个项目为它;
1)ServiceLibrary(WCF库)
2)网络
3)ConsoleTestClient
我ServiceLibrary
app.config文件看起来是这样的;我如何使用WCF数据服务?
<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.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>
现在,当我直接使用(ServiceLibrary项目)使用WCF测试客户端测试该服务,我看到下面和工程一切伟大的;
的问题是,当我尝试运行我的Web
项目(我为WCF服务的主机使用)。然后转到控制台测试客户端,并想使用添加引用添加引用。我看不到我的Get
和Set
方法(如测试客户端) 为什么我看不到我的IAdvertisementService接口和方法
我是不是要部署这在实际工作IIS?
一篇文章已删除:
更新:
在Microsoft Developer Network实际上涵盖本很详细,一些他们提供的资源有:
也有几本书,我解决这个特殊的努力。由于有人表示提供链接来解决这个问题并没有真正回答你的问题,我会尝试。
- Visual Studio内部的点击文件,然后进行新建项目。
- 在该对话框中展开的Visual C#,选择网络和ASP.NET Web窗体应用程序。
- 给你的项目一个你自己选择的名字
NorthwindWeb
。
此时您已经创建了一个项目;由于复杂的服务俯视一个微小的细节可能是灾难性的结果。这就是我从零开始的原因。
在我的示例中,我将其链接到一个数据库。所以我会添加一个Ado.Net实体数据模型。我会命名我的模型:NorthwindModel
。我也将基于现有的数据库生成。所以在这一点上,只需按照Visual Studio向导。在这些表中选择您的数据库对象,然后单击完成。
重要的部分,建立我的Data Service
。
- 项目添加新项目。
- 选择网络并选择WCF数据服务。
- 加一个名字,
NorthwindCustomer
- 然后加。
找到第一Todo:
评论和删除代码,然后放:
public class DemonDbCustomer : DataService<demonDbEntities>
然后发现在InitializeService
事件处理的意见:
config.SetEntitySetAccessRule("*", EntitySetRights.All);
此时打CTRL + F5运行该服务。浏览器将打开并生成服务的XML模式。在地址栏输入客户在URL的末尾NorthwindCustomers.svc
并点击输入。
**有时Internet Explorer会搞砸了,所以可能需要额外的故障排除。 **
现在我们将创建我们的客户端部分。
- 添加一个新建项目
- 选择Windows窗体应用程序
- 名称您所选择的文件,
NorthwindClient
然后单击确定。 - 在Solution Explorer中选择
NorthwindClient 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;
现在,在您Solution Explorer中右击该NorthwindCustomers.svc
,并单击浏览器查看。 XML Schema将被添加,因此您只需从地址栏复制该URL。然后将Uri
替换为您刚刚复制的那个。
运行你的应用程序,你已经下了以下内容:
- 主机
- 客户
- 服务
- 消耗
那是怎样消耗WCF数据服务更详细的文章是here:
希望有帮助。
使用WCF数据服务感谢您的建议。但是,在iis express工作上托管wcf服务? – 2013-03-06 18:08:56
嗯,我相信Windows Vista,Windows 7,Windows Server 2008,Windows Server 2008 R2和Windows Server 2012都使用IIS:6,7,7.5/8我相信天性。但是,如果您只有IIS Express,那么只要将IIS配置为处理这些服务请求,它就会工作。 – Greg 2013-03-06 18:11:25
我已经看过一些来自packtpub和wrox的书 – 2013-03-06 18:16:15
要使用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应用程序的根目录中,程序集将被复制到bin目录中。应用程序可以使用服务文件的URL访问。
WCF服务可以托管在IIS或WindowsActivationService中。
将服务类型编译到类库中 将扩展名为.SVC的服务文件复制到虚拟目录中,并汇编到虚拟目录的bin子目录中。 将web.config文件复制到虚拟目录中。
客户端开发
客户端的ASP.NET Web服务使用命令行工具生成WSDL.EXE。
WCF使用ServiceMetadata工具(svcutil.exe)为服务生成客户端。
更多细节转到此链接 http://www.codeproject.com/Articles/139787/What-s-the-Difference-between-WCF-and-Web-Services
AdvertisementService不是一个WCF服务,它的一个WCF数据服务。完全不同的东西。您希望通过[DataServiceContext](http://msdn.microsoft.com/zh-CN/library/system.data.services.client.dataservicecontext.aspx) – Aron 2013-03-06 18:23:01