Attaching WCF DLL to existing Website - c#

Okay I've created a WCF which I have tested in in IIS and works correctly.
There is a website running, and I've done the following to add this service to that domain:
-Copied the WCF DLL into the website's bin folder.
-Also copied all the DLL this service uses. (Just Newtonsoft.Json)
-Created a new .svc with the following line:
<%# ServiceHost Language="C#" Debug="true" Service="wcfOnScore.OnScoreQuery"%>
-And also added the following to the website's web.config file:
<System.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="WithDebug">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpBinding" scheme="http" />
</protocolMapping>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" minFreeMemoryPercentageToActivateService="0" />
<services>
<service name="wcfOnScore.OnScoreQuery">
<endpoint address="http://www.onscore.co.za/OnScoreQuery.svc"
binding="basicHttpBinding"
listenUri="/"
contract="wcfOnScore.IOnScoreQuery" />
</service>
</services>
</system.serviceModel>
Now this seems to be working. I get the following page when I open it in the browser:
Link To The Screenshot
It is however, not working sadly. I am trying to consume this service from a c# Xamarin android application. It is supposed to return a JSON string. And does this fine when it is hosted locally - This leads me to believe the problem is not in the WCF but the way I am trying to host it.
Anything I might have missed? Or any other alternatives?
Thanks in advance people.
EDIT:
Thought I'd include the error.
"System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation."
The is what the completed eventhandler arguments returns.

Well this an answer to why I get the error, not really a solution. (Yet)
I commented out the Newtonsof.Json using statement. And put in a simple text return which worked.
So from this I deduce that the WCF code can't get its hands on the json.dll.
So it looks like all the above was done correctly if you would like to use as reference. (Except that .dll off course).

If you are trying to return JSON first test it on browser. Your service is SOAP based service because you are using basicHttpBinding. To get result on browser change the binding to WebHttpBinding and add following in your web config.
<endpointBehaviors>
<behavior name="RestBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
If you are able to get response on the browser, you will get response through android also.

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!)

add service reference only works with localhost wcf service

I am trying to host a svc service in my web application and I am trying to connect to it with a simple client application.
When the host is hosted in my own computer and solution, and I run the web application locally, everything works fine, meaning that I can add the reference to the service using add service reference in my client solution with http://localhost:6543/Hello.svc as the address and when I navigate to the service's url, it shows me the service's information as it should.
But when I publish my website, and I try to add the reference to my service using add service reference with http://myserver.com/Hello.svc as the address, i get the error message bleow:
There was an error downloading
'http://myserver.com/Hello.svc/_vti_bin/ListData.svc/$metadata'. The
request failed with HTTP status 404: Not Found. Metadata contains a
reference that cannot be resolved: 'http://myserverc.com/Hello.svc'.
The remote server returned an unexpected response: (405) Method Not
Allowed. The remote server returned an error: (405) Method Not
Allowed. If the service is defined in the current solution, try
building the solution and adding the service reference again.
and oddly when I navigate to myserver.com/Hello.svc, the browser starts to download the Hello.svc file instead of showing the service's details and information.
this is my website's web.config file:
<system.serviceModel>
<bindings>
<webHttpBinding>
<binding name="crossDomain" crossDomainScriptAccessEnabled="true" />
</webHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="WithDebug">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
<services>
<service name="HelloWorldService.HelloService" behaviorConfiguration="WithDebug">
<host>
<baseAddresses>
<add baseAddress="http://myserver/Hello.svc"/>
</baseAddresses>
</host>
<endpoint
address=""
binding="webHttpBinding" bindingConfiguration="crossDomain"
contract="HelloWorldService.IHelloWorld" />
<endpoint
address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange" />
</service>
</services>
</system.serviceModel>
I think I am missing something big here, but since I am new to WCF, I don't know what it is.Any help would be appreciated and I am more than happy to issue more details about my project if needed.
Thanks in advance
You need to run ServiceModel registration tool
"This command-line tool provides the ability to manage the registration of WCF and WF components on a single machine. Under normal circumstances you should not need to use this tool as WCF and WF components are configured when installed. But if you are experiencing problems with service activation, you can try to register the components using this tool."

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 :)

Displaying WCF Web service operations

So I've created a WCF service application and hosted it on IIS7. It currently has a few test 'helloworld' methods. When I run it in my browser I get this screen:
Now the service itself works great, but how can I display the operations like this:
Thanks to marc_s for the link: http://www.dotnetcurry.com/ShowArticle.aspx?ID=399 which I've followed so my web config is now setup like:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<services>
<service name="WcfServer.Service1">
<endpoint address="" binding="webHttpBinding" contract="WcfServer.IService1" behaviorConfiguration="HelpBehaviour" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- 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="false" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="AjaxBehavior">
<enableWebScript />
</behavior>
<behavior name="HelpBehaviour">
<webHttp helpEnabled="true"/>
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
<directoryBrowse enabled="true" showFlags="Date, Time, Size, Extension" />
</system.webServer>
</configuration>
However, this only works locally. When I publish to my server on IIS7 I get a 404 error page when I click on the help link. Does anyone know why this is, or has come across it before?
(Last bit was solved by running: aspnet_regiis.exe -iru)
If you have a WCF service with a SOAP binding, you're unfortunately out of luck: there's no way in WCF out of the box to get a listing similar to ASMX with all the services.
With REST binding (webHttpBinding) and .NET 4.0, you can have an automatic help page generated which lists the URI templates, the HTTP methods supported and so forth. You can also tweak that page to a certain degree.
In order to have that automatic help page generated, you need to define (and reference) an endpoint behavior:
<behaviors>
<endpointBehaviors>
<behavior name="HelpBehavior">
<webHttp helpEnabled="true" />
</behavior>
</endpointBehaviors>
</behaviors>
Then reference that behavior from your webHttpBinding endpoint, and you're done.
Read all about it:
A Developer's Introduction to Windows Communication Foundation 4
Windows Communication Foundation 4.0 - New REST Features
Clients and the Automatic Help Page in WCF WebHttp Services

WCF + Wildcard Mapping + IIS6 = 404s on Every Method!

I am receiving 404s when executing a WCF method on IIS6 when wildcard mapping is enabled.
You can all reproduce this by creating a new WCF Service in VS2008 (new Project > WCF Service Application). Browse to the dummy method ('GetData')... you will notice it returns 400... that's fine because it shows it's still forwarding to WCF.
However: if you enable wildcard mapping in IIS6 you will now get a 404, meaning WCF is no longer intercepting the request.
My code is as follows:
[ServiceContract]
public interface IRest {
[OperationContract]
[WebGet(UriTemplate = "/test")]
int Test();
}
With the following web.config:
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="ServiceX.RestBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="ServiceX.RestBehavior">
<serviceMetadata httpGetEnabled="false" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="false" />
<services>
<service behaviorConfiguration="ServiceX.RestBehavior"
name="ServiceX.Rest">
<endpoint address="" behaviorConfiguration="ServiceX.RestBehavior"
binding="webHttpBinding" contract="ServiceX.IRest" />
</service>
</services>
</system.serviceModel>
All works fine without wildcard mapping; I can browse to '/services/rest.svc/test' and I'll receive the expected result.
However, as soon as I enable wildcard mapping (.* > C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll), then I start receiving 404s when I attempt to access a method (although I can still view '/services/rest.svc').
I've exhausted Google and StackOverflow :(
I just had the same problem with a WCF service running on IIS6.
I could browse the service on http://someurl/service.svc, but I would get a 404 when hitting a method on the service such as http://someurl/service.svc/somemethod.
The fix, in my case, was easy. The .svc file type was configured in IIS to be handled by C:\Windows\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll, but my service was running in a ASP.NET v4.0 apppool, so I simply pointed the .svc file type to be handled by C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll

Categories

Resources