Cannot access WCF service from IE - wcf

This has probably been discussed however all the threads I saw on this topic did not help me hence I'm posting.
I am attempting to host a WCF HTTP service on IIS 5.1. It is extremely basic. I created a virtual directory in IE called wcftest, which points to the actual folder containing the contents of the service. Here is the structure:
Web.config
ConsoleWCF.svc
[App_Code] \ Program.cs
Here is the code for ConsoleWCF.svc
<%# ServiceHost Debug="true" Service="ConsoleWCF.WCFImplementer" Language="C#" %>
The Web.config looks like this:
<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior">
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="ConsoleWCF.WCFImplementer"
behaviorConfiguration="ServiceBehavior">
<endpoint
address=""
binding="basicHttpBinding"
contract="ConsoleWCF.WCFInterface" />
<endpoint
address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange" />
</service>
</services>
</system.serviceModel>
</configuration>
And finally the Program.cs is:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.ServiceModel.Description;
namespace ConsoleWCF
{
[ServiceContract]
public interface WCFInterface
{
[OperationContract]
[WebGet]
string GetData();
}
public class WCFImplementer : WCFInterface
{
public string GetData()
{
return "Tested Console WCF";
}
}
}
I can access this in IE by browsing to http://localhost/wcftest/ConsoleWCF.svc.
However I do not understand how to access the GetData method I have. When I did this using a stand-alone host, I had no issues accessing it. Any help is greatly appreciated.

In order for WCF to understand the [WebGet] attribute, the endpoint needs to have a certain configuration, namely use the webHttpBinding (not basicHttpBinding as you do) and to have a WebHttpBehavior added to it. If you change the web.config to something like the one below, you should be able to browse to http://localhost/wcftest/ConsoleWCF.svc/GetData.
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="REST">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<service name="ConsoleWCF.WCFImplementer">
<endpoint address=""
behaviorConfiguration="REST"
binding="webHttpBinding"
contract="ConsoleWCF.WCFInterface" />
</service>
</services>
</system.serviceModel>

Related

How to return an xml response from a WCF Service

I'm not fluent in WCF Services so I'm struggling to return XmlElement as the return type.
I'm getting the message from the WCF Test Client (running in debug mode):
The operation is not supported from the wcf test client because it uses type XmlElement.
[ServiceContract]
public interface IClientService
{
[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Xml)]
XmlElement GetClientXml(int value);
}
namespace testWCF
{
public class testSvc: IClientService
{
public XmlElement GetClientXml(int value)
{
string appDir = AppContext.BaseDirectory;
XmlDocument xDoc = new XmlDocument();
xDoc.Load(appDir + #"Xml\ResponseTempl.xml");
return xDoc.DocumentElement;
}
}
}
I've referred to this as well but it might be too old, as I'm using 4.6.1 framework: Returning XML From a WCF Service
my Web.Debug.config file :
<?xml version="1.0" encoding="utf-8"?>
<!-- For more information on using web.config transformation visit https://go.microsoft.com/fwlink/?LinkId=125889 -->
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<system.web>
</system.web>
<system.serviceModel>
<services>
<service name="AucklandRegionalPatientWCF.PatientDemographicService" >
<!-- these endpoint are necessary to return SOAP service -->
<endpoint address=""
binding="basicHttpBinding"
contract="AucklandRegionalPatientWCF.IPatientDemographicService" />
<endpoint address="mex"
contract="IMetadataExchange" binding="mexHttpBinding"/>
<!-- REST service return xml -->
<!--To call this endpoint use: [service].svc/xml/[method_Name]-->
<endpoint address="xml"
binding="webHttpBinding" behaviorConfiguration="xmlBehavior"
contract="AucklandRegionalPatientWCF.IPatientDemographicService" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<!-- use XML serialization -->
<behavior name="xmlBehavior">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
</configuration>
Best way to get XML element from WCf as response use XML formatted string.. Best at performance as well as light weight.....
Create response class and use XML serialization.... It will be helpful

Metadata publishing for this service is currently disabled - Again

I'm new to WCF and I've been hitting my head for the past week trying to get everything to work. When browsing the service.svc file I receive the message about the metadata not being enabled. There's hundreds of posts on this but I must be missing something. I think I followed the instructions correctly but I still can't find my error. Where am I going wrong? Any help is appreciated.
service.svc
<%# ServiceHost Service="BiteSizeLearningWS.TranscriptService" Debug="true" %>
web.config
<services>
<service name="BiteSizeLearningWS.iServiceInterface" behaviorConfiguration="TranscriptServiceBehavior">
<endpoint address="" binding="basicHttpBinding" contract="BiteSizeLearningWS.TranscriptService" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="TranscriptServiceBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
ServiceContract
namespace BiteSizeLearningWS
{
[ServiceContract (Name="TranscriptService")]
public interface iServiceInterface{...
Implementation
public class TranscriptService : iServiceInterface
Global.asax
namespace BiteSizeLearningWS
{
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
RouteTable.Routes.Add(new ServiceRoute("TranscriptService", new WebServiceHostFactory(), typeof(TranscriptService)));
}
I think you have your:
<service name="BiteSizeLearningWS.iServiceInterface"...
name attribute value and the
<endpoint address="" ... contract="BiteSizeLearningWS.TranscriptService" />
contract attribute value mixed up. Try this:
<service name="BiteSizeLearningWS.TranscriptService"...
and
<endpoint address="" ... contract="BiteSizeLearningWS.iServiceInterface" />
If that works, then what happened is that WCF used the automatic default configuration values for the service instead of the invalid configuration shown in the question. The metadata endpoint is not enabled by default which would be why you're seeing the "disabled" message.

WCF Service host using ServiceHostFactory with Multiple Binding

I have created WCF service to host on IIS.
I am using ServiceHostFactory method to host my service(using Unity as DI).
I want to host my service using multiple binding, over the HTTP as well as over the TCP.
I tried giving base address, but its not taking. still giving me error as Service registered with HTTP schema.
Below code snippet might give you and idea.
public class MyServiceHostFactory : ServiceHostFactory
{
protected override ServiceHost CreateServiceHost(
Type serviceType, Uri[] baseAddresses)
{
MyServiceHost serviceHost = new MyServiceHost(serviceType, baseAddresses);
//configure container
MyFactory.Register();
return serviceHost;
}
}
Config file:
<system.serviceModel>
<services>
<service name="My.Service.MyService">
<host>
<baseAddresses>
<add baseAddress="http://localhost:9000/MyService/"/>
<add baseAddress="net.tcp://localhost:9001/MyService/"/>
</baseAddresses>
</host>
<endpoint address="Question" binding="basicHttpBinding" contract="My.Contract.IQuestionContract" />
<endpoint address="Answer" binding="basicHttpBinding" contract="My.Contract.IAnswerContract" />
<endpoint address="Question" binding="netTcpBinding" contract="My.Contract.IQuestionContract" />
<endpoint address="Answer" binding="netTcpBinding" contract="My.Contract.IAnswerContract" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="MyServicebehavior">
<serviceMetadata httpGetEnabled="True" httpsGetEnabled="True" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="True">
</serviceHostingEnvironment>
Can anyone suggest me what can be done?
Main thing,
Is it possible to host service using ServiceHostFactory with Multiple Binding?
If Yes, can anyone help me how?
I'd try to remove base net.tcp address and specify full address on endpoint.

WCF Error : Relative end point addresses

Um quite new to WCF . I think I have messed up a bit. So this is what I did so far and I ve hosted my WCF service in IIS
First the Contracts
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using YangkeeServer.dto;
namespace YangkeeServer
{
public class Service1 : IService1
{
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, UriTemplate = "single")]
public YangkeeTrailerDTO getTrailor()
{
return new YangkeeTrailerDTO()
{
loanFrom = "heaven"
};
}
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "datum/{id}")]
public Test getName(string id)
{
return new Test()
{
Id = Convert.ToInt32(id) * 12,
Name = "Leo Messi"
};
}
}
}
and this is my web.config file
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<services>
<service name="YangkeeServer.Service1">
<endpoint
contract="YangkeeServer.IService1"
binding="webHttpBinding"
behaviorConfiguration="WebHttp"
address="http://localhost:7000/Service1.svc">
</endpoint>
</service>
<service name="YangkeeServer.Service2">
<endpoint
contract="YangkeeServer.IService2"
binding="webHttpBinding"
behaviorConfiguration="WebHttp"
address="http://localhost:7000/Service2.svc">
</endpoint>
</service>
<service name="YangkeeServer.YangkeeTrailer">
<endpoint
contract="YangkeeServer.IYangkeeTrailor"
binding="webHttpBinding"
behaviorConfiguration="WebHttp"
address="http://localhost:7000/YangkeeTrailor.svc">
</endpoint>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="WebHttp">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
and um using this urlhttp://localhost:7000/Service1.svc and um getting this error
Server Error in '/' Application.
When 'system.serviceModel/serviceHostingEnvironment/multipleSiteBindingsEnabled' is set to true in configuration, the endpoints are required to specify a relative address. If you are specifying a relative listen URI on the endpoint, then the address can be absolute. To fix this problem, specify a relative uri for endpoint 'http://localhost:7000/Service1.svc'.
can any one tell me where did I do wrong? Thank you in advance.
Set the service endpoint address to just the service filename (this is the relative part):
address="Service1.svc"
or set the address to blank for each endpoint (I don't think its needed when hosting in the development environment or in IIS)
address=""
See this answer to another question
The relevant part is:
the virtual directory (in IIS) where your *.svc file exists defines your service endpoint's address.
To fix this error, I added... listenUri="/" ... to the service endpoint.
Example:
<service name="_name_">
<endpoint address="http://localhost:_port_/.../_servicename_.svc"
name="_servicename_Endpoint"
binding="basicHttpBinding"
bindingConfiguration="GenericServiceBinding"
contract="_contractpath_._contractname_"
listenUri="/" />
</service>
MSDN: Multiple Endpoints at a Single ListenUri
This works for me:
1. Insert this lines inside service:
<host>
<baseAddresses>
<add baseAddress="http://localhost:7000/Service1.svc" />
</baseAddresses>
</host>
2. Set the value of address as bellow:
address=""
The full code:
<service name="YangkeeServer.Service1">
<host>
<baseAddresses>
<add baseAddress="http://localhost:7000/Service1.svc" />
</baseAddresses>
</host>
<endpoint
contract="YangkeeServer.IService1"
binding="webHttpBinding"
behaviorConfiguration="WebHttp"
address="">
</endpoint>
</service>

How to use a WCF service with HTTP Get (within Visual studio 2010)

We've tried to use a very very simple WCF service with a HTTp Get and we can't get it work.
We've followed those "guide" but it doesn't work
http://msdn.microsoft.com/en-us/library/bb412178.aspx
http://www.dotnetfunda.com/articles/article779-simple-5-steps-to-expose-wcf-services-using-rest-style-.aspx
When we call our service with the following url, we get a page not found error:
http://localhost:9999/Service1.svc/GetData/ABC
The base url (http://localhost:9999/Service1.svc) works fine and returns the wcf service information page correctly.
Those are the steps and code to reproduce our example.
In Visual Studio 2010, create a new "WCF Service Application" Project
Replace the IService interface with this code
[ServiceContract()]
public interface IService1
{
[OperationContract()]
[WebInvoke(Method = "GET",
BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate = "GetData/{value}")]
string GetData(string value);
}
Replace the Service class with this code
public class Service1 : IService1
{
public string GetData(string value)
{
return string.Format("You entered: {0}", value);
}
}
The web.config look like this
<system.web>
<compilation debug="true" strict="false" explicit="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<services>
<service name="Service1">
<endpoint address="" binding="webHttpBinding" contract="IService1" behaviorConfiguration="WebBehavior1">
</endpoint>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="WebBehavior1">
<webHttp helpEnabled="True"/>
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
Press Run and try to call the Get method
If someone get this or something similar working, it would be very kind if you could reply information about the working example.
Thank you very much
I recreated your sample - works like a charm.
One point: do your service contract (public interface IService1) and service implementation (public class Service1 : IService1) exist inside a .NET namespace??
If so, you need to change your *.svc and your web.config to include:
<services>
<service name="Namespace.Service1">
<endpoint address="" binding="webHttpBinding"
contract="Namespace.IService1"
behaviorConfiguration="WebBehavior1">
</endpoint>
</service>
</services>
The <service name="..."> attribute and the <endpoint contract="..."> must include the .NET namespace for this to work.