how to use WCF for web application - wcf

i need to use WCF Service with in my web application.
I ama new to wcf.
i created the service and add the service reference in my web application.
It shows the http:localhost in end point address.
endpoint address="http://localhost:52123/GeneralService1.svc"
i am confused to use the local host for access the wcf in web.
Is there any way to communicate with wcf / how to enable the connection.
Thanks in Advance
Pooja

A WCF endpoint has its ABC.
A - Address
B - Binding
C - Contract
<endpoint address="" binding="wsHttpBinding" contract="WcfService2.IService1">
You can change the address, by putting the Url of your deployment server, where the service would be hosted in production.
Read this for more details of WCF endpoint ABC

You can modify this address in the web.config file, to the url, while deployment.

Related

Service vs Client nodes/sections in Web.Config

What is the difference between the Service node/section and the Client node/section in the configuration section? Why configure endpoints in one section over the other? Which is best for interoperability?
I'm currently building a service that talks to another service. I have endpoints for my clients and endpoints for the other service. Visual Studio seems to lump all the endpoints into the Client section.
I thought that client node was for your consumption and service node was for producing. But when you create a new wcf service visual studio puts your new service endpoint settings under the client node. I have moved my endpoint between both sections trying to figure out what the difference is.
When should I use service over client?
<system.serviceModel>
<services>
<service> <!--I noticed some tutorials and using wcf config edit tool
puts producer endpoint settings here -->
<endpoint blah settings/>
<endpoint blah settings/>
</service>
</services>
<client> <!--Visual Studio puts both producer and consumer endpoint
settings here -->
<endpoint blah settings />
<endpoint blah settings />
<endpoint blah settings />
</client>
<bindings>.....
</system.serviceModel>
Many settings in the WCF web.config (or app.config for that matter) can be shared for both consumers of a service as well as publishers of a service including:
Bindings
Endpoint Behaviors
Diagnostics
However as you have discovered, some config is specific to a service. A well-written service usually specifies:
It's base address. This is a convenience when defining a service as it allows your to define endpoints using relative addresses. Clients however don't use this particular setting as they need an absolute path. For this reason it makes no sense to specify in the section
Services can also be clients. If the client and server endpoints were all plonked together, WCF would not be able to know which should utilise the base address for one thing
Service behavior
By dividing up config between client and server, WCF is better able to know where to look for endpoints.
Which is best for interoperability?
I don't think that has anything to do with it. WCF is a means to achieve interopability but just by using WCF does not imply you will achieve it. Interopability is established when both parties agree on say a particular service contract; canonical data model; data transformation; message version or many of the other patterns as defined by SOA Patterns.org So there are various patterns you must follow. e.g. If you change a method on service contract but have not updated the clients then you have broken interopability by breaking the schema of the service.
Visual Studio seems to lump all the endpoints into the Client section
If your WCF process is both a consumer and producer of WCF services then it should not be putting all the endpoints under

Regarding WCF endpoint address attribute

being new to wcf i often stuck to write endpoint address in config file. sometime i just could not understand what address i should write in endpoint at host end where service will be running. here is one sample endpoint entry....please have a look.
<endpoint address="net.tcp://localhost/ServiceModelSamples/Service1.svc"
binding="netTcpBinding"
contract="ServiceReference1.IService1">
</endpoint>
<endpoint address="net.tcp://localhost/ServiceModelSamples/service"
binding="netTcpBinding"
contract="ServiceReference1.IService1" />
i have couple of question on endpoint address
1) when i should write endpoint address like this way
net.tcp://localhost/ServiceModelSamples/Service1.svc
i guess when we will host our service in IIS then we need to write endpoint address like above one where we need to write our svc file name.
am i right?
2) just see the below address
address="net.tcp://localhost/ServiceModelSamples/Service1.svc"
what is ServiceModelSamples do i need to mention it?
3) suppose i develop wcf service with wcf service application template and host that service in win form apps then how our endpoint address would look like below one
address="net.tcp://localhost/ServiceModelSamples/service"
do i need to write ServiceModelSamples our project name folder or we can remove it from
address like address="net.tcp://localhost/service"
it is very important for me to know that do i need to write our project folder name like
ServiceModelSamples
4) when we host wcf service in win form apps then do i need to specify our svc file name with extension.
5) specifically guide me what address i need to write when we host our service in win form apps.
my all above question may sounds very stupid but i have really confusion about address in
endpoint. i have no there way to put question here. So please have a look at my 5 points and guide me. thanks
I think your confusion comes from the differences between IIS Hosting and custom hosting.
When you use IIS to host the service, the service behaves this way:
A base address is assigned to the service automatically depending on where the service is hosted in IIS. It uses the same address mapping that a web site uses.
The service layer needs to have .svc files to expose service instances through IIS. Those service files are just mapping files that help exposing on IIS.
So to fill the address to a service hosted in IIS from the client you need to:
Find out the address that has been assigned by IIS.
Complete that address with the Service.svc file name that you want to point to.
However when using custom hosting (through ServiceHost) the service address is totally goberned by the service configuration file:
The service can configure a base address to all its endpoint via the baseAddresses setting
All endpoints can have a full address or relative address. If relative address is used then the resulting endpoint address will be the baseAddress + endpoints relative address.
When custom hosting you have complete control over the service addresses, so you don't have any of the constraints that you must follow on IIS.

What is the use of WCF endpoint address

I am new in WCF. I know that we have to write endpoint in config file at service end and as well as client end. suppose I have multiple endpoint like
<services>
<service name="YourNamespace.YourService" behaviorConfiguration="Default">
<endpoint name="Default"
address="http://YourServer/Services/MyService"
binding="basicHttpBinding"
contract="YourNamespace.IYourService"/>
<endpoint name="TCP"
address="net.tcp://YourServer/ServicesTCP/MyService"
binding="netTcpBinding"
contract="YourNamespace.IYourService"/>
<endpoint name="Dual"
address="http://YourServer/Services/MyService/Dual"
binding="wsDualHttpBinding"
clientBaseAddress="http://localhost:8001/client/"
contract="YourNamespace.IYourDualService"/>
<endpoint name="mex"
address=""
binding="mexHttpBinding"
contract="IMetadataExchange"/>
</service>
</services>
we know that we can not create proxy at client side with endpoint address like
http://YourServer/Services/MyService/Dual or
net.tcp://YourServer/ServicesTCP/MyService
rather if we need to create proxy at client side then we need to provide mex endpoint address. so I just do not understand what is the use of endpoint address?
When we create proxy at client side and call service then we just do not understand our proxy is using what endpoint address to connect to service?
That is why I just want to know how endpoint address come into role?
I know that we can write separate mex endpoint for tcp in config file as a result we can create proxy with that mex url as a result when client would connect to service then tcp protocol will be used for communication but for other http endpoints one mex endpoint works fine.
My important question is which I really like to know that suppose i have 3 endpoints like basichttp,wshttp,wsdualbidning then one mex endpoint works for all of them to create proxy. so tell me in that case when client connect to service then which endpoint address will be used to connect to that service?
It will be great help if some one discuss this issue with great detail and with sample config entry and as well as sample service code?
UPDATED Part
Tom Redfern said...service endpoints is not required in case of internal use. suppose I have developed a service which is hosted in console apps and other client need to connect to that service. so tell me in this case how client can connect to service without proxy class and call various method of service. I just like to know without proxy how can I connect and call various method of wcf service. please come with some sample code for client side just to show how programmatically I can connect and call various method of wcf service without proxy.
An endpoint needs a way of addressing it. This is both fundamental and reasonable.
Your argument that the client only requires a metadata endpoint address in order to resolve the actual service endpoints only holds true when you are exposing a metadata endpoint (which is by no means required) and when the consumer has no other means to consume the service (perhaps the service is public).
Most services are developed for internal consumption where the ability to bind directly to an endpoint via referencing a shared types assembly (rather than via a service proxy) is commonplace. Knowledge of the endpoint address in these instances is absolutely required.
If you read about the history of UDDI, this was designed as a means to distribute service metadata to consumers who would have no need to know anything else about the service. However, how often do you see a UDDI server? I have seen it used in exactly one company (I have worked in about 20 in total).

How to change the WCF Service Url?

I have a WCF 3.0 service which has been configured with BasicHttpBinding and has been deployed and hosted in IIS.
I can access my service using the below url:
http://mydomain.com/subfolder1/subfolder2/myservice.svc/basic
"basic" at the end of the address is the relative address of the end point and "/subfolder1/subfolder2" is where the myservice.svc file has been physically deployed to:
<endpoint address="basic" binding="basicHttpBinding"
contract="Company.MyService.Implementation.Contracts.IMyService">
</endpoint>
How to configure the settings so that I can access my service using the below url without specifiying the .svc path:
http://mydomain.com/myService/basic
Thanks,
In fact, it is not the responsability of your service. You should build "address-less" service and not specify one absolute in the config.
Your question is relevant to web hosting under IIS. There is a huge difference between the physical path (where you deploy) of your service and the virtual path (where client access it).
In your example, you simply need to create a new web application under http://mydomain.com/ and publish your service. subfolder1 seems to be a virtual directory of an existing web site. Just create the web app here, with IIS manager.

Using WCF services remotely

I have created a simple WCF service by following a MSDN tutorial. I successfully created service and client. But I have a very basic question, that how can I use this service remotely. Foe example my service is hosted on a web server then how can I access and consume it from my PC. I know how to do this with web services but don't know with WCF service as Iam new to WCF. Any tutorial or code sample is appreciated.
Thanks
In your sample you must have specified the address of the service as endpoint.
You just need to modify the address in your app/web.config file and it will start talking to remote service.
See Specifying an Endpoint Address
Once you deploy the WCF service on some remote server all you have to do is to modify the endpoint address of the client to point to this remote address. This will depend on where you have configured the client endpoint. Usually it is done in the app.config/web.config:
<endpoint address="http://someremotedomain/myservice.svc" ...