IIS hosted wcf returns me blank page - c#

I have a wcf application. It has "Service1.svc" file. In the web.config file I specified
http://localhost:2005/EmployeeService.svc
as an endpoint. When clicking browse from Visual Studio there is no problem. But, when I hosted it on IIS server I get a blank page. The interesting thing is, If I remove the address from the web. config this time I can see the service at this address.
http://localhost:2005/EmployeeService.svc
web.config file is as below:
<?xml version="1.0"?>
<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="EmployeeServiceBehaviour">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="EmployeeServiceBehaviour" name="EmployeeConfiguration">
<endpoint address="http://localhost:2005/EmployeeService.svc" binding="basicHttpBinding"
bindingConfiguration="" contract="IEmployeeConfiguration" />
</service>
</services>
</system.serviceModel>
<system.web>
<compilation debug="true"/>
</system.web>
<system.webServer>
<directoryBrowse enabled="true"/>
</system.webServer>
</configuration>
Could you please explain, why I get a blank page on IIS when I provide the adress.

This may happen in HTTP Activation is not installed in Programs and Features in your Windows installation. Check HTTP Activation for both .Net 3.5 and 4.6 to enable opening WCF service via HTTP request to .svc file hosted in IIS.

I think you can remove all text on the address before Service.svc. This might be an issue with different urls.
When you specify the address part, you tell WCF the service is only available on that address. When going through the endpoints, WCF will not find one that matches the given URL and bail out.
When you have multiple endpoints you need the address field. Otherwise you don't.
Read http://msdn.microsoft.com/en-us/library/ms733749(v=vs.110).aspx
And in particular this part:
When hosting with IIS, you do not manage the ServiceHost instance yourself. The base address is always the address specified in the .svc file for the service when hosting in IIS. So you must use relative endpoint addresses for IIS-hosted service endpoints. Supplying a fully-qualified endpoint address can lead to errors in the deployment of the service.

Check if Service attribute in SVC markup equals your implementation class.
Check endpoint contract name. It has to be exactly full name of your interface. Don't add assembly name, it won't work.
Check <serviceMetadata httpGetEnabled="true" />. If false, you get blank page too.
And 4th change which helped me (but I have no idea why): Remove behavior name <behavior name=""> and remove behaviorConfiguration attribute from service element. It becomes default so the behavior will be used anyway.

Be sure you made an Application Pool with active user like this: https://stackoverflow.com/a/19654633/2148387
Be sure you created Web Application where your WCF is hosted
Check if AppPool and WebApp are running
Enable Directory Browse in your Web.config like here: https://stackoverflow.com/a/19630263/2148387

Related

My wcf client side shows exception but WcfTestClient Tool works fine

I have coded a WCF server side with netTcpBinding. Then I coded a client side. But it shows exception while excute "var sc = new CommondServiceClient();" at runtime. What should I do?
Below is the exception message:
System.InvalidOperationException
HResult=0x80131509
Message=Could not find default endpoint element that references contract 'ICommondService' in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element.
Source=System.ServiceModel
StackTrace:
......
I have tried something:
I can consume the services with WcfTestClient.
The service reference is added by visual studio "add service reference...". I guess I get the service mex data. But I meet runtime exception like above
I also tried generate code with svcutil tool, but it still not work
Here is the wcf config file:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" />
</system.web>
<!-- When deploying the service library project, the content of the config file must be added to the host's
app.config file. System.Configuration does not support config files for libraries. -->
<system.serviceModel>
<services>
<service name="ServiceContractor.CommondService">
<endpoint address="" binding="netTcpBinding" contract="ServiceContractor.ICommondService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information,
set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="False" httpsGetEnabled="False"/>
<!-- 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="False" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
It's a self host wcf service:
var baseAddress = new Uri($"net.tcp://localhost:{PORT}/Company/service");
_host = new ServiceHost(typeof(CommondService), baseAddress);
try
{
var smb = _host.Description.Behaviors.Find<ServiceMetadataBehavior>();
if (smb == null) _host.Description.Behaviors.Add(new ServiceMetadataBehavior());
_host.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexTcpBinding(), "mex");
_host.AddServiceEndpoint(typeof(ICommondService), new NetTcpBinding(), "");
_host.Open();
}
catch (CommunicationException ce)
{
_host.Abort();
}
I have no idea what is wrong. What document I should ask for? Can you help me?
But I have another question now. Is it possible to get rid of the configuration. So that the application just needs the DLL and knows nothing about the WCF service.
Generally speaking, there are two ways to call the WCF service (Soap service) in the web application.
Generate the client proxy class by adding service reference, this way
commonly need to configure the service settings in the configuration
file(web.config), most of service information need to be called are
stored in the configuration file. For class library project, we have
to migrate the configuration to the configuration file of the actual
project.
https://learn.microsoft.com/en-us/dotnet/framework/wcf/accessing-services-using-a-wcf-client
Use the ChannelFactory to create the communication channel, we set
up the service configurations programmatically. From your
description, this way to call the web service might be the
appropriate solution. Customize the service configuration
programmatically in the code. One thing must be noted that all the
service configuration is hard-coded, such as binding type and service
endpoint information. You could refer to the following documents.
https://learn.microsoft.com/en-us/dotnet/framework/wcf/feature-details/how-to-use-the-channelfactory
Feel free to let me know if there is anything I can help with.

Hosting in IIS (503 Service Unavailable)

I am trying to host my WCF service in IIS 7.5, but everytime i run the website i get a page stating
Service Unavailable
HTTP Error 503. The service is unavailable.
I noticed that whenever I started the website the ASP.net v4.0 stopped automatically. I checked the eventviewer and this seems to be the error
The worker process for application pool 'ASP.NET v4.0' encountered an error 'Configuration file is not well-formed XML
' trying to read configuration data from file '\?\C:\Windows\Microsoft.NET\Framework64\v4.0.30319\CONFIG\web.config', line number '0'. The data field contains the error code.
I tried searching for solutions around the internet but none of them have been helpful so far.
I tried changing the identity of ASP.net v4.0 in IIS to LocalSystem and it didn't work
After that i tried setting up a "Custom account" by setting the username as my computer username and adding passwords but i got "The specified password is invalid, type a new password" even though i don't have any passwords for my account
3.The final thing i tried was to add IIS_IUSRS and IUSR to the security of the Web.config but still it didn't work
This is my Web.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="mexBehavior">
<serviceMetadata httpGetEnabled="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="mexBehavior"
name="HelloService1.HelloService1">
<endpoint address="HelloService1" binding="basicHttpBinding"
contract="HelloService1.IHelloService1" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8080" />
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
</configuration>
Try a re-install of the .NET Framework 4.0. Go to this location
%windir%\Microsoft.NET\Framework64\v4.0.30319
Run this command
aspnet_regiis -i

How do i Consume WCF Data Service?

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)
Why i don't see my IAdvertisementService interface and the methods
Do i have to deploy this to actuall IIS?
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
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.

WCF .svc worked but not the config file

I am trying to use the configuration file to define endpoint and services information. I have a very simple code that contain OneWay service and a Duplex service. The OneWay worked when I haven't try to alter the configuration file.
Now, I want to use the configuration file to define both service.
Service1 contract name is IOneWayService and the Service2 contract name is ICallBackService.
Both have implemented code in their concrete respective classes name OneWayService.svc.cs and CallBackService.svc.cs.
The configuration file at this moment look like that :
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true">
<serviceActivations>
<add relativeAddress="OneWayService.svc" service="TestingWcf.OneWayService"/>
<add relativeAddress="CallBackService.svc" service="TestingWcf.CallBackService"/>
</serviceActivations>
</serviceHostingEnvironment>
<services>
<service name="TestingWcf.OneWayService">
<endpoint address="http://localhost:60847/One"
binding="wsHttpBinding"
contract="IOneWayService" />
</service>
<service name="TestingWcf.CallBackService">
<endpoint address="http://localhost:60847/Two"
binding="wsHttpBinding"
contract="IDuplexService" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
I always have this error when trying to execute the OneWayService via this url : http://localhost:60847/OneWayService.svc
The contract name 'IOneWayService'
could not be found in the list of
contracts implemented by the service
'OneWayService'.
Anybody have an idea why?
Edit
I have removed the multipleSiteBindingsEnabled= true from the servinceHostingEnvironment tag and in the contract added the namespace and I could runt the OneWayService.
Also, the Duplex cannot be bound to the wsHttpBinding. I had to change it to NetTcpBinding. But, I had an other error with the Duplex :
Configuration binding extension
'system.serviceModel/bindings/NetTcpBinding'
could not be found. Verify that this
binding extension is properly
registered in
system.serviceModel/extensions/bindingExtensions
and that it is spelled correctly.
From this point, I am lost again.
Edit 2
I did an error in the binding name. I had a capital letter for NetTcpBinding and it does require a lowercase: netTcpBinding. However, it's still not working, now I have:
The protocol 'net.tcp' is not
supported. >.< !!!
OK, that explains it - Visual Studio by default uses the built-in Cassini web server (unless you've already switched to using IIS Express) - and that server doesn't support anything but plain http.
Cassini doesn't support net.tcp and anything like that.
You will need to start using a separate IIS virtual directory and first enable all the necessary support stuff (in the Add/remove Windows Features dialog)

WCF and Multiple Host Headers

My employers website has multiple hostnames that all hit the same server and we just show different skins for branding purposes.
Unfortunately WCF doesn't seem to work well in this situation.
I've tried overriding the default host with a custom host factory.
That's not an acceptable solution because it needs to work from all hosts, not just 1.
I've also looked at this blog post but either I couldn't get it to work or it wasn't meant to solve my problem.
The error I'm getting is "This collection already contains an address with scheme http"
There's got to be a way to configure this, please help :)
If you don't put an address in the endpoint then it should resolve to whatever server hits the service. I use this code and it resolves both to my .local address and to my .com address from IIS.
<system.serviceModel>
<services>
<service name="ServiceName" behaviorConfiguration="ServiceName.Service1Behavior">
<endpoint address="" binding="wsHttpBinding" contract="iServiceName">
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceName.Service1Behavior">
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above 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>
I don't think that the host base addresses solution posted above will work for IIS-hosted websites (the OP did mention that this was for his employer's website).
See this blog post
Also, the other answer further up by thaBadDawg won't work where multiple host headers are specified - you'll just get the errors that the OP mentions ("This collection already contains an address with scheme http".)
I don't think any of the solutions mentioned so far will work, because it doesn't look like WCF allows a single service to be accessible for a single site with multiple host headers from all of the sites. The only workaround I could find for .Net 3.5 (and under) is to create a different contract for each of the host headers, and use the custom ServiceHostFactory to use the correct host header based on which contract is specified. This isn't at all practical. Apparently .Net 4.0 will resolve this issue.
I met this problem several days ago. Actually I have the same situation that Ryu described originally in his question. We have one virtual directory for many customers but each of them has his own binding. Like "http://company1.product.com", "http://company2.product.com" etc.
Solution described here works. But what is the price! We should change web.config each time when we need to add new binding. And also web.config should contain absolute path prefix
like <add prefix=”http://company1.product.com”/>.
It is possible to bypass first problem. I wrote my own CustomHostFactory for WCF service, where I dynamically add endpoints. And this endpoints I retrieve from IIS bindings(there is a way to get information from IIS).
Here is sample code :
protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
{
var serviceHost = base.CreateServiceHost(serviceType, baseAddresses);
var webHttpBinding = new WebHttpBinding();
var serviceEndpoint1 = serviceHost.AddServiceEndpoint(typeof(IService), webHttpBinding,
"http://company2.product.com/WCFService/Service.svc");
var serviceEndpoint2 = serviceHost.AddServiceEndpoint(typeof(IService), webHttpBinding,
"http://company1.product.com/WCFService/Service.svc");
var webHttpBehavior = new WebHttpBehavior();
serviceEndpoint1.Behaviors.Add(webHttpBehavior);
serviceEndpoint2.Behaviors.Add(webHttpBehavior);
return serviceHost;
}
And instead of hardcoding endpoint urls, you sould retrieve them from IIS.
But ServiceHost is created once when application starts. So if you need to add new binding, you should restart IIS. It is not a solution for us.
That is why we decided to move to asmx (like is described here).
And wait until Framework 4.0 release, where multiple bindings should be supported.
A simple workaround that doesn't involve any code or config changes is to create yet another website in IIS which points to the same physical directory, and the same app pool, but with a different host-header binding. This can be done for as many different host names as you have.
I'm sure you've figured it out by now, but just for fun I'll post here anyway.
I've had this exact problem and spent forever trying to figure it out. The best solution is to put the host base addresses in your service definition, which allows the service to operate under these multiple addresses. For this solution to work, you still need the ServiceHostFactory to be overridden. Since you've already done that, just leave it in there.
<service behaviorConfiguration="ServiceBehaviour" name="Api.Service">
<endpoint address="soap" binding="basicHttpBinding" contract="Api.IService" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://host1.com/Api" />
<add baseAddress="http://host2.com/Api" />
</baseAddresses>
</host>
</service>

Categories

Resources