问题
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;
<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
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)

Do i have to deploy this to actuall IIS?
回答1:
To develop a service using ASP.NET, we must add the WebService attribute to the class and WebMethodAttribute to any of the class methods.
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:
[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.
A class that implements the service contract is referred to as a service type in 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.
WCF Service can be hosted within IIS or 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.
Client Development
Clients for the ASP.NET Web services are generated using the command-line tool WSDL.EXE.
WCF uses the ServiceMetadata tool (svcutil.exe) to generate the client for the service.
For more detail goto this link http://www.codeproject.com/Articles/139787/What-s-the-Difference-between-WCF-and-Web-Services
回答2:
Previous Post Removed:
Update:
The Microsoft Developer Network actually covers this in great detail, some of the resources they provide are:
- Overview
- Tutorial From Code Project
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 Web and ASP.NET Web Forms Application.
- Give your project a name of your choice
NorthwindWeb
.
At this point you've created a project; due to the complexity of a Service overlooking 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 Objects within those tables, then Finish.
The important part, building my Data Service
.
- Project Add New Item.
- Select Web and choose WCF Data Service.
- Put a name,
NorthwindCustomer
- Then Add.
Locate the first Todo:
Comment and remove the code then put:
public class DemonDbCustomer : DataService<demonDbEntities>
Then find the comments in the InitializeService
Event Handler:
config.SetEntitySetAccessRule("*", EntitySetRights.All);
At this point hit CTRL + F5 to run the service. The browser will open and the XML Schema for the service will generate. In the Address Bar type in Customers at the end of the URL for NorthwindCustomers.svc
and hit Enter.
** Sometimes Internet Explorer will mess this up, so additional troubleshooting may be required. **
Now we will create our Client Portion.
- Add a New Project
- Select Windows Forms Application
- Name the file of your choice,
NorthwindClient
then click Ok. - In the Solution Explorer select the
NorthwindClient Project
and Set As Startup Project. - Right Click on Project: Add Service Reference Click Discover.
At this point your URL for NorthwindCustomers Service
will appear in that Address field. This is generated from that .svc
file.
Now we have to provide data binding to our service.
- On the Data Menu we 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.
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
.
DataGridView
BindingSource
BindingNavigation
They are all added automatically. Then simply double click your Form
and add the following to your 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 Explorer right 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.
Run your application and you've down the following:
- Host
- Client
- Service
- Consumed
That is how to consume a WCF Data Service the article that has even more detail is here:
Hopefully that helps.
来源:https://stackoverflow.com/questions/15254251/how-do-i-consume-wcf-data-service