Supporting WCF in ASP.NET MVC - c#

I'm trying to make the same project to work with WCF and MVC.
My problem is:
MVC is working perfectly, than I included the interface and the .svc that I had in WCF service.
When I try something like this:
http://localhost:2986/PAGENAME.svc
I get the following error:
The resource cannot be found.
NOTE: PAGENAME.svc is in root (and so as the interface).
Looking forward this problem, I included the ignore methods in RegisterRoutes:
routes.IgnoreRoute("{resource}.svc/{*pathInfo}");
routes.IgnoreRoute("{resource}.svc");
But didn't work either =/
Does anyone know how to fix this?
Thank you!

You need to make sure that you have all the files required, which are referenced from the Service Host (.svc file), i.e.:
<#% ServiceHost Service="..."/>
Where Service specifies the service implementation.
The service contract (the interface that the service implementation implements) is usually configured in web.config.
You don't need to ignore the route if the service host file is at the root of your solution.
You need to reference System.ServiceModel.
If you want to test your service you can by opening visual studio command prompt and running wcftestclient, File -> Add service and add the url for your service, e.g.:
http://locahost:12423/MyService.svc

It's been a while since I've played with this, but I think when using MVC you need to register a service route... but I don't remember if that's what I had to do or if I just wanted to do that for cleaner routes.
To add a service using a service route, you would do something like the following
routes.Add("MyService", new ServiceRoute(
"some/path",
new ServiceHostFactory(),
typeof(MyService)
));

Related

Error when trying to add a Service Reference

I´m trying to create a client in C# to a web service which (I suppose) is written in Java. It´s my first time trying to write a client, so I´m following the instructions on MSDN, but I´m stuck on a problem with Add Reference. When I open the Add Service Reference dialog and add the URL, an error occurs:
There was an error downloading 'http://geoportal.cuzk.cz/WCTService/WCTService.svc'.
The request failed with HTTP status 404: Not Found.
Metadata contains a reference that cannot be resolved: 'http://geoportal.cuzk.cz/WCTService/WCTService.svc'.
There was no endpoint listening at http://geoportal.cuzk.cz/WCTService/WCTService.svc that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.
The remote server returned an error: (404) Not Found.
If the service is defined in the current solution, try building the solution and adding the service reference again.
What should my next step be? I don´t know what I should do with this!
(It is a coordinates-transformation service from the Czech Republic.)
For more information:
Property services (GetCapabilities)
http://geoportal.cuzk.cz/WCTService/WCTService.svc/get?
Localization services:
http://geoportal.cuzk.cz/WCTService/WCTService.svc/get?request=GetCapabilities&service=WCTS
I was facing a similar situation in which I had created a WCF Service (Employee.svc) and later changed the named to EmployeeService.svc. WCF project compiled just fine but when I was trying to add service reference from by UI Client, I was getting following error:
Metadata contains a reference that cannot be resolved: 'http://localhost:2278/EmployeeService.svc?wsdl'.
The document format is not recognized (the content type is 'text/html; charset=UTF-8').
Metadata contains a reference that cannot be resolved: 'http://localhost:2278/EmployeeService.svc'.
There was no endpoint listening at 'http://localhost:2278/EmployeeService.svc' that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.
The remote server returned an error: (404) Not Found.
If the service is defined in the current solution, try building the solution and adding the service reference again.
I resolved it by replacing the correct service class name everywhere. In my case, it should have been EmployeeService and NOT employee. The left out place was in the markup code of svc file:
<%# ServiceHost Language="C#" Debug="true" Service="WCFServiceHost.**Employee**" CodeBehind="EmployeeService.svc.cs" %>
Changed it to
<%# ServiceHost Language="C#" Debug="true" Service="WCFServiceHost.**EmployeeService**" CodeBehind="EmployeeService.svc.cs" %>
And it started working again!!! Dont forget to build your WCF project after changing the service name.
I tried browsing to http://geoportal.cuzk.cz/WCTService/WCTService.svc?wsdl. It looks like this service is not exposing metadata.
I did a bit of googling on OpenGIS, and I think you need to have a look at this article:
OpenGIS with .NET
You won't be able to just add a service reference and go. It looks like you need to craft a concrete WSDL.
There may be a client-side library you can use / customize to assist with integration. Have a look at Stack Overflow question Using MySQL GeoSpatial data types in .NET.
I know this is an old thread and has already been resolved, but I just finished troubleshooting this exact issue, and none of the resolutions presented here worked for me. Wanted to share my resolution in case anyone else runs into this thread with a similar issue.
My ENTIRE issue stemmed from a bad Refactor->Rename operation. I recently purchased Resharper for my dev team and Resharper did not like the name of our service implementation name. We had named it "WCFAccess" and Resharper wanted the name "WcfAccess". I had just published an update, had the release safely isolated in its own release branch in git, and figured this was a good time to perform a rename on the develop branch and shut Resharper up about the naming. I used Refactor->Rename to change the name of the file to match the naming convention we had defined in the Resharper configuration. The rename operation completed, the solution compiled and ran, time goes on and the WCF rename was forgotten.
Fast forward a couple weeks, and its time to deploy out web services to the test environment for regression testing. The solution compiled successfully, published successfully, then gave me the EXACT error that the OP posted. What I ended up finding out is that the Rename operation from weeks ago ONLY UPDATED SOURCE CODE REFERENCES to the old name and did not rename MARKUP. When I navigated out to our web server where the service was published to and double clicked on the .svc file, it opened the markup in Visual Studio and I noticed that the character casing of the CodeBehind="ServiceNameHere.svc.vb" was inconstant with the new naming convention. Updating the markup and web.config files to reference the correct character casing resolved my issue.
I hope this helps someone. It was incredibly frustrating to troubleshoot
(Please don't hate me for using VB.Net, I inherited this application) :-)
Is the service definitely up and running before you try to add a service reference? If it exposes meta data, does it have a service behaviour or equivalent configured? Have you configured your firewall correctly?
While adding service reference to the client application, metadata is not accessible from service to client application. hence The remote server returned an error: (404) Not Found. Actually we can host the WCF service as follows:
Self hosting(console application)
IIS Hosting
WAS Hosting
window service hosting.
if you are using self hosting then you need to host the service in console application and run the service(run the console application) and then add the service reference to the client application, then metadata would be exchange. If service is not running then while adding service reference to the client application then 404 not found error would be getting. Same process would follow for all the hosting type. first run the service then add service reference.
I had the same problem happen to me earlier today. The webservice was running fine on local host but for some reason, I was having a 400 when trying to add the service reference in another project.
My error was caused by setting the [DataMember] annotation instead of the [EnumMember] annotation on an enum of the service. Changing it solved my issue.
The webservices doesn't run.
If you don't have access to the server where this service run, you're blocked.
Otherwise, you need to check if the server run, etc. As I don't know how the Java webservice is run, I can't help you further.

Magento Go C# requesting API fails to initialize

I am trying to connect to Magento API using C#. I am using Magento Go service and from what I've read I am able to use their API - I hope I am not wrong here. So here is what I did:
I added a Service Reference to http://mydomain.gostorego.com/api/v2_soap?wsdl=1, and just adding a service worked fine. Now I created a test class with GetStuff() method, which looks like this:
using ww.Feeds.MagnetoGoService;
public static string GetStuff()
{
MagnetoGoService.Mage_Api_Model_Server_V2_HandlerPortTypeClient s = new MagnetoGoService.Mage_Api_Model_Server_V2_HandlerPortTypeClient();
var login = s.login("username here", "key here");
return login.ToString();
}
When I run the program I get an error in first line saying:
Could not find default endpoint element that references contract 'MagnetoGoService.Mage_Api_Model_Server_V2_HandlerPortType' 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.
Any ideas what this may be? Do I have to set something up in my Magento Go settings? Or maybe using Magento Go is not allowing API access?
Thanks a lot.
Forget SOAP with c# you will pull your hair out. Download Charls Cook's xml-rpc api libary for c# and use the xml-rpc method. You won't get all the snazzy intellisense but at least it will work. There's also c# solution from ez.newsletter they released with Cook's library demonstrating how to use 80% of the magento api calls.
Cook's library xml-rpc.net
http://www.xml-rpc.net/
ez.newsletter solution
http://code.google.com/p/csharlibformagexmlrpcapi/
If anyone ever has problems with this, my solution was this:
I used the reference in one project, but I actually called the class and had main program in another project. You need your Service reference to be in each project wherever you're using it. That fixed it! Alternatively you can create a new BasicHttpBinding() and putt all the options from app.config/web.config into that binder, then you don't need to reference to Service everywhere. I hope that helps!

How to override target URI for a web service reference in VS 2008

This seems like it should be really simple, but I'm unable to figure this out.
I am adding a web service reference to my console application. The web service points against our production environment and I would like to test it against development. In VS2005 it was really easy to override the target URI of the service. Is it possible to do the same with VS2008? I would like to set the URI in code or via a config file.
I would really appreciate help with this. Thanks!
If you look in your .config, you should see <endpoint> elements that were added to the <client> section of <system.serviceModel>. Assuming you need to point at only one environment at a time you can simply edit the address attribute of those endpoints to point to whatever URL you want.
If you wanted to change this at runtime you need to use the constructor overload for the client proxy that was created. There should be several overloads that take a parameter called remoteAddress in many forms.
It seems that I was able to get around this issue by going to "Add Service Reference" -> "Advanced" -> "Add Web Reference". This gives me a reference in the form I am most familiar with. Now I was able to override the constructor with the URI parameter. Is that method frowned upon?

WCF and moving the proxy code to a DLL. Is it possible?

it looks like I am not able to succesfully move my WCF proxy code into a separate DLL (as opposed to an EXE as I can see in all the examples I have run into).
The reason I am trying to do this is that I would like my proxy code to be invoked by different clients (possibly unmanaged code), which might not know anything about WCF but just need to access to the services (through a Facade exposed by the proxy maybe?).
Whenever I move the following code that creates a new proxy to a different VS project within the same solution, I get the dreaded "Could not find default endpoint element that references contract 'localhost.IRemoteCommandService' in the ServiceModel client configuration section" exception.
localhost.RemoteCommandServiceClient proxy =
new localhost.RemoteCommandServiceClient();
The same code works smoothly whenever used within a Main method in the same project where the proxy code is (auto-generated from Visual Studio).
Any idea? I hope that the client code of my proxy does not need to have the service model XML configuration as the proxy, because that would defeat the purpose I am moving the WCF proxy code into a DLL in the first place.
Thanks,
Stefano
The endpoints are indeed normally specified in the configuration file. You must look at the serviceModel data in the config file, and copy it into your calling app.config - or you need to use the more verbose way of creating the proxies in your code (i.e. specifying the address, binding, configuration etc through code to the constructors).
If you don't want to have to endpoint configuration on the client, you'll have to embed it into your proxy dll by specifying everything in code.
Another option would be to use a dynamic proxy, like this one, which would allow you to not have the serviceModel in your client apps.

Forcing WcfSvcHost.exe to use my custom service host

Is it possible to force WcfSvcHost (which is executed automatically when I do an F5 or when I am debugging another project in the solution) to use a custom ustom service?
I have my custom service host working great in my asp.net Host container by using a service factory which in turn calls the Custom Service Base.
But when WcfSvcHost executes it's not using my custom ustom service.
Is this possible?
If not, what are my alternatives? I presume I must uncheck "Start WCF service host when debugging a project in another solution" which is in the WCF Options in app properties but then I must create a console Host container?
And I can't get the console host container to automatically execute each time I am debugging something else?
I notice this under DEBUG in app properties (maybe I can use something like this to force the loading of the custom servicehost)
/client:"WcfTestClient.exe"
The problem being is that I have my custom ServiceHost inject some UNITY (IOC) stuff, here the overriden method ... so it must execute otherwise it fails.
protected override void InitializeRuntime()
{
Bootstrapper.ConfigureUnityContainer();
base.InitializeRuntime();
}
I was trying exactly the same thing for exactly the same purpose (;-)
I thought I found a solution by not using physical .svc files anymore (which contain the custom host factory when hosting in IIS), but moving this info to the .config file instead:
<serviceHostingEnvironment aspNetCompatibilityEnabled="false">
<serviceActivations>
<add relativeAddress="~/Services/NaisTime/NaisTimeService.svc" service="Nais.Time.Services.NaisTime.NaisTimeService"
factory="Nais.Time.Services.NaisServiceHost.NaisServiceHostFactory, Nais.Time.Services" />
<add relativeAddress="~/Services/Northwind/NorthwindService.svc" service="Nais.Time.Services.Northwind.NorthwindService"
factory="Nais.Time.Services.NaisServiceHost.NaisServiceHostFactory, Nais.Time.Services" />
</serviceActivations>
</serviceHostingEnvironment>
It works for IIS, but putting the same entries in the app.config file of my Service Library project does not make SvcWcfHost use this.
I guess I am not getting my relativeAddress right.
Anybody any experience with this?
kr,
Michel Liesmons.
I don't think you can do that - you'll need to host in IIS or create your own, customized service host.

Categories

Resources