Localhost WCF service unavailable after stopping debug - c#

I have problem reaching my WCF service on localhost. When I start it from Visual Studio, everything is fine. However, as soon as I stop debugging it, the service is no longer available. This is odd, because it happens only with projects created by myself. When I download a solution (e.g. this one), everything is just fine and the service is available also after I stop debugging it.
Thus, I suppose, it is a configuration problem. However, I am not able to find it. Here is my Web.config file:
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5"/>
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name ="svcbh">
<serviceMetadata httpGetEnabled="False"/>
<serviceDebug includeExceptionDetailInFaults="False"/>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name ="DuplexServer.CalculatorService" behaviorConfiguration ="svcbh" >
<host>
<baseAddresses>
<add baseAddress = "http//localhost:3435/CalculatorService/" />
</baseAddresses>
</host>
<endpoint name ="duplexendpoint"
address =""
binding ="wsDualHttpBinding"
contract ="DuplexServer.ICalculatorDuplex"/>
<endpoint name ="MetaDataTcpEndpoint"
address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange"/>
</service>
</services>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>

It sounds like the settings for your solution/project is not what you desire. Go the properties of your WCF project and look at "Web" settings(plenty of information on the net about this)
You will probably need to change from "Use Visual Studio Development Server" to "Use local IIS Web Server"

Assuming you are using IIS Express, it's usually caused by Edit and Continue (Right click the project, select properties. It's a checkbox in the Web tab).
Disable this, and the service should keep running when you stop debugging.
On a related note, if you wish to simply start the service to have available (i.e. you don't want to debug, you just want it running), you can use "Start without Debugging" in the Debug menu (CTRL + F5 by default)

Related

Service Only Works While Debugging

I have created a WCF service and am having some trouble testing it once it has been deployed. Here is the PowerShell I am using to test it:
$service = New-WebServiceProxy -Uri http://localhost:16651/Service.svc
$service.GetList()
When debugging the service from Visual Studio with F5, I can call this script without any issue. GetList() returns a long list of telephone numbers.
However, when I host the site on IIS and run the above script, I get an empty return value.
Service Factory
So following this question, I added this attribute to Service.svc:
Factory="System.ServiceModel.Activation.WebScriptServiceHostFactory"
However, this resulted in my script returning an error on the first line:
New-WebServiceProxy : Object reference not set to an instance of an object.
Which does not make any sense to me, as I am not referencing any empty objects... (this error appears when debugging and when hosting over IIS).
Web.Config
Next, I tried updated my web.config as per the linked question:
<services>
<service name="LyncWebService.Service">
<endpoint binding="webHttpBinding" contract="LyncWebService.IService" behaviorConfiguration="web"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
However, now when I try to run my PowerShell script I get this error both during debugging and when hosting on IIS (again on the first line):
The HTML document does not contain Web service discovery information.
I am totally lost here and have no idea what is going wrong. I suspect it is to do with my config file, as it did seem to work when debugging from VS before I messed with the configuration.
Any help or guidance is much appreciated - and please let me know if I can provide any other information or test anything.
Here is the code that makes up my service currently:
Service.svc.cs
namespace LyncWebService
{
[ServiceContract]
public interface IService
{
[OperationContract]
[WebInvoke]
List<string> GetList();
}
public class Service : IService
{
public List<string> GetList()
{
return Ps.GetAssignedNumbers(#"
Set-ExecutionPolicy RemoteSigned
Import-Module Lync
$(Get-CSUser).LineUri"
);
}
}
}
Web.Config
<?xml version="1.0"?>
<configuration>
<appSettings/>
<system.web>
<compilation debug="true" targetFramework="4.0"/>
<httpRuntime/>
</system.web>
<system.serviceModel>
<services>
<service name="LyncWebService.Service">
<endpoint binding="webHttpBinding" contract="LyncWebService.IService" behaviorConfiguration="web"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<protocolMapping>
<!--<add binding="basicHttpsBinding" scheme="https"/>-->
<add binding="webHttpsBinding" scheme="https"/>
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="false" multipleSiteBindingsEnabled="true"/>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<!--
To browse web app root directory during debugging, set the value below to true.
Set to false before deployment to avoid disclosing web app folder information.
-->
<directoryBrowse enabled="true"/>
</system.webServer>
</configuration>
Thanks to Jonathan Coffey, I realised that the service was being run by the LocalSystem account.
After changing this to my own user account and hosting the original web.config on IIS, I am now able to retrieve the full list using my PowerShell script.
Open IIS
Application Pools
Right-Click the Application pool
Advanced Settings...
Process Model -> Identity
Custom Account (Don't forget to include the domain for the User Name!)

Trouble with WCF and WAS with web site and web service in separate application pools

None of the members of my team has ever been able to get a particular WCF service in our solution to work on our local machines. We've inherited it in legacy code and are trying to replace it, but it's very difficult to tell what it's doing since we can't run the debugger on it and we can't even get a response from it while debugging the main site that uses it.
The main part of our application is a web site. This particular service is hosted in a separate application pool on IIS due to some problems with using Excel interops (which this service uses) in the same app pool as the main site.
The service appears to use net.tcp for the protocol, and I have enabled the Windows Communication Foundation Non-HTTP Activation feature on my machine. I have also enabled the protocol on the Default Website and the node underneath it which is the WCF service in question (is this redundant?).
I can attach the debugger to w3wp.exe processes for both the site and the service. When the site makes the call to the service, however, an error is immediately returned and no breakpoints in the service are hit. The error reads:
The service 'MySvc.svc' cannot be activated due to an exception during
compilation. The exception message is: The type 'MyNamespace.MySvc',
provided as the Service attribute value in the ServiceHost directive,
or provided in the configuration element
Note, I have obviously redacted the real service name, etc, from this post. After attempting to follow solutions proposed on numerous similar questions, I have gotten nowhere. I am wondering if the problem is exacerbated by the separate app pools.
Below is the Web.config from the service project. The SVC file is named UploadAndImport.svc.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<configSections>
<!-- REDACTED SECTIONS PERTAINING TO LOGGING AND ENTERPRISE LIBRARY -->
</configSections>
<!-- REDACTED SECTIONS PERTAINING TO LOGGING AND ENTERPRISE LIBRARY -->
<system.web>
<compilation debug="true" strict="false" explicit="true" targetFramework="4.6.1" />
<identity impersonate="true" />
<pages controlRenderingCompatibilityVersion="4.0" />
</system.web>
<system.serviceModel>
<services>
<service behaviorConfiguration="ServiceBehavior" name="ProjectName.UploadAndImport">
<endpoint address="" binding="basicHttpBinding" bindingConfiguration="" name="ExcelServiceEndPointHTTP" contract="ProjectName.IUploadAndImport" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<endpoint binding="netTcpBinding" bindingConfiguration="" name="ExcelServiceEndPointTCP" contract="ProjectName.IUploadAndImport" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior">
<!-- 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>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
<!-- This line ignores the error that 'An ASP.NET setting has been detected that does not apply in Integratd managed pipeline mode (system.web/identity#impersonate is set to true)'-->
<validation validateIntegratedModeConfiguration="false" />
</system.webServer>
</configuration>
The directive in the SVC file looks like this:
<%# ServiceHost Language="VB" Debug="true" Service="ProjectName.UploadAndImport" CodeBehind="UploadAndImport.svc.vb" %>

IIS hosted WCF Service return HTTP 400 Bad Request

I have been searching for hours, but I could not find the solution. I will explain briefly.
I am learning WCF Services. I have just created a service and browse it. Here is the config file:
<?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>
When browse it from Visual Studio there seems no problem. It works perfectly.
Second, I am trying to publish it on IIS. What I am doing is this:
I publish the service to a folder and add this service to IIS.
I select port 3006 as a port.
Below its config file. Note that I also changed port inside config to 3006
<?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:3006/EmployeeService.svc" binding="basicHttpBinding"
bindingConfiguration="" contract="IEmployeeConfiguration" />
</service>
</services>
</system.serviceModel>
<system.web>
<compilation/>
</system.web>
<system.webServer>
<directoryBrowse enabled="true"/>
</system.webServer>
</configuration>
And I am waiting to run smoothly but:
IIS gives me a blank page from Chrome
And HTTP 400 Bad Request from Explorer
Lastly, if I remove address part from config file everything works well. But other confused thing is that, on my other computer after above scenario(address provided) I can reach the service. So, I really tired of searching why this is working on one computer and not working on another one. Could someone explain it to me?
I know it is a bit longer, but I have to explain it clearly.
Thanks
According my searches, I should not provide an address.
msdn.microsoft.com/en-us/library/aa751792(v=vs.110).aspx
You must always use relative endpoint addresses for IIS-hosted service
endpoints. Supplying a fully-qualified endpoint address (for example,
localhost/MyService.svc) can lead to errors in the deployment of the
service if the endpoint address does not point to the IIS-application
that hosts the service exposing the endpoint. Using relative endpoint
addresses for hosted services avoids these potential conflicts.
I think this will solve your problem:
Add this endpoint to your service:
<endpoint address="mex" binding="mexHttpBinding"
bindingConfiguration="" contract="IMetadataExchange" />
And change the name attribute of the service to your service class's full name:
<service behaviorConfiguration="EmployeeServiceBehaviour"
name="Namespace.EmployeeConfigurationClass">
Hope that is enough
This may be of help. ive just spent over 2 hours trying to get this working. i use FF and its set as the default browser.
in FF it was adding a / on the end of my URL
http://services.tester.dev/VehicleFeedService.svc/
which returned a NetworkError: 400 Bad Request
however in IE or chrome, it doesnt put the / on the end and it works fine.
one thing to note.. even in FF which was giving me a 400 bad request, the ?wdsl did work
http://services.tester.dev/VehicleFeedService.svc?wsdl
it appears that the / was causing the issue
You can try fiddler and also try the svcTracer which may give you lot of debugging information on the top of it you can also use includeExceptionDetailInFaults=true flag on the server but its important to flag that its not always right to send this information to the client specially if client is an external entity. With this warning following is the hint how to use it.
<serviceBehaviors>
<behavior name="ServiceBehavior">
....
<serviceDebug includeExceptionDetailInFaults="true" />
....
</behavior>
</serviceBehaviors>
Happy debugging :)

C# WCF cant see app.config

Ok so here is my scenario.
I have made a WCF Library project. Configured my app.config file. I then created a new console application. Added my WCF Library project to the console app. Then I copied the config file over to my server console app. However, when I run the console application it throws an exception and essentially states that it cant see the app.config file. However, the app.config is clearly inside of the server console application. I can add my endpoints programmatically and make the service work. However, that is not my intention. Is there some sort of trick in order to get the ServiceHost to use the app.config, or more importantly the project to see the app.config?
My app.config
<configuration>
<configSections>
</configSections>
<system.web>
<compilation debug="true" />
</system.web>
<system.serviceModel>
<services>
<service behaviorConfiguration="serviceBehavior" name="Services">
<endpoint address="CompanyService" binding="netTcpBinding" bindingConfiguration=""
name="NetTcpBinding" contract="Services.ICompany" />
<endpoint binding="mexHttpBinding" name="mex" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:8732/Services/" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="serviceBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
My Host Service code:
using (ServiceHost host = new ServiceHost(typeof(Company))) {
host.Open();
Console.WriteLine("Press <ENTER> to terminate the host service");
Console.ReadLine();
}
It then throws this exception:
Unhandled Exception: System.InvalidOperationException: Service 'Services.Company' has zero application (non-infrastructure) endpoints. This might
be because no configuration file was found for your application, or because no
service element matching the service name could be found in the configuration fi
le, or because no endpoints were defined in the service element.
What are you naming the app.config file when you copy it over to the console app? There are specific rules that will need to be followed.
If the console app has an executable name of, e.g. "consoleapp.exe", then the app.config will have to have the name "consoleapp.exe.config"
Is the problem that you have called the file app.config, and not yourprogramname.exe.config?
#Jaochim The error says it's looking for a service named Services.Company while your config seems to have it attributed with name="Services" Try changing that.

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)

Categories

Resources