WSDL-First approach, not same WSDL deployed - c#

Here is my approach (don't hesitate to tell me if I am doing something wrong) :
-Write XSD files defining my objects
-Use WSCF.blue to generate WSDL accordingly to the XSDs
-Use WSCF.blue to generate Web Service Code
-Implementing a stub and exposing the SVC
So far, I am not facing any problem. I can access to the .svc through my browser. But the thing is the deployed WSDL is not the same as the designed one.
When I tried to test the service with SOAP UI and the designed WSDL as source, it failed because the WSDL are differents. When I tried with the deployed one, it works fine.
Same result when I tried to generate a client (console application) with the designed WSDL (with SvcUtil.exe) : it fails the same way (ContractFilter mismatch at the EndpointDispatcher exception). it works I add a service reference.
I won't develop the client but the people they will will work on a WSDL I had to give them first. Is there a way to work with the designed WSDL or I had to give them the deployed one ?
Thanks in advance.
Excuse me for my english, I am not a native speaker.

Yes you can expose your own wsdl like this (using the example data from the wscf.blue walkthrough), with externalMetadataLocation being the important part:
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="MyBehavior">
<serviceMetadata httpGetEnabled="true" externalMetadataLocation="../ContractMetaData/RestaurantService.wsdl"/>
</behavior>
</serviceBehaviors>
</behaviors>
...
<services>
<service name="RestaurantService.RestaurantService" behaviorConfiguration="MyBehavior">
...
</service>
</services>
</system.serviceModel>
But I haven't had any luck making this work in conjunction with "add service reference". VS keeps generating code that isn't compatible with the webservice generated by wscf.blue.

Related

c# - trying to build a web service client from WSDL but no config file is provided

I'm just getting familiar with WCF and I have to add additional functionality to a working web service at work. As I feel the need to be able to test the functionality before deploying it, I decided to build a test client. Here comes the problem.
I created a Console Application just for the purpose of a test client and tried to add a Service Reference through the provided WSDL but it didn't work. There was no config file created.. I tried first the "Add Service Reference" option in VS and when it didn't work, I tried creating the Proxy and Config files with svcutil.exe...
Just the proxy class gets created... When I try to instantiate a "client object" from that class, the following Exception is thrown: "Could not find default endpoint element that references contract 'eOrderingService.IeOrderingService' 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."
As this is a working service (a Czech company is using it), apparently there must be a way to create a web.config or app.config even manually but I have no idea where to start.. As I said I'm just getting familiar with WCF so I started looking online but most of the problems connected somehow to my issue were in different parts of the already created Config files.. I managed to bypass that exception adding the following to app.config:
<system.serviceModel>
<services>
<service name="eOrderingService" >
<endpoint
address="http://localhost:61472/eOrderingService.svc"
binding="webHttpBinding"
contract="eOrderingService.IeOrderingService" >
</endpoint>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp helpEnabled="true" />
</behavior>
</endpointBehaviors>
</behaviors>
<client>
<endpoint
address="http://localhost:61472/eOrderingService.svc"
binding="webHttpBinding"
bindingConfiguration=""
contract="eOrderingService.IeOrderingService"
behaviorConfiguration="web"
name="DeliveryNote" >
</endpoint>
</client>
The service has a lot of methods and the one I need to test is called 'DeliveryNote'.
So lets say the service is at this address:
http://localhost:61472/eOrderingService.svc
The POST method I need to call is:
http://localhost:61472/eOrderingService.svc/DeliveryNote
And respectively the GET method is:
http://localhost:61472/eOrderingService.svc/DeliveryNote?DocumentFilter={DOCUMENTFILTER}&CustomerID={CUSTOMERID}&FromDate={FROMDATE}
The links are working but I cannot figure out how to call them from the client.
When I tested calling the POST method I received another exception:
The remote server returned an unexpected response: (400) Bad Request.
That shouldn't be true because the request I'm sending is already tested and is a valid request in XML format.
So I tried to test with a different GET method that works and receives just two DateTime parameters and not a Xml. If I try the following link:
http://localhost:61472/eOrderingService.svc/PriceChanges?startDate=2018-08-29&endDate=2018-08-30
the result is OK..
But if I call the automatically generated method "PriceChanges" the result is NULL.
I just don't get what I do wrong. It seems like a connection to the service is established but the methods are not called/build correctly. Probably because I cannot comprehend how to build the <system.serviceModel> in app.config.
I definitely should read more about the web services but I don't know where to start.

Solution with MVC/WebApi and WCF - Authentications

I have been assigned a old project that currently uses a WCF service.
The point is to update (more like starting fresh) the project to use ASP.NET MVC5 and Web API. The problem is that a 3rd party uses the WCF service, and they are probably not willing to make changes to their end of the communication.
The main function of the project is two basic, one is receive data, save it and just show the last status of several subjects and history graphs, and the other is to send data (turn on/off subjects).
My idea was to maintain the WCF Service (receive/send/save data) as is, just add it to new solution which consists of MVC and Web API (read data). They need (I think) to be in the same project, because the final objective is to implement SignalR on the WCF Service, if possible.
The main problem, is that the MVC and WebAPI are going to stay behind Authentication, but the WCF won't. At the moment when I try to test the WCF on the same project, it fails because it asks for a "Log in."
Error: Cannot obtain Metadata from
http://localhost:50598/ServiceTest.svc If this is a Windows (R)
Communication Foundation service to which you have access, please
check that you have enabled metadata publishing at the specified
address. For help enabling metadata publishing, please refer to the
MSDN documentation at
http://go.microsoft.com/fwlink/?LinkId=65455.WS-Metadata Exchange
Error URI: http://localhost:50598/ServiceTest.svc Metadata contains a
reference that cannot be resolved:
'http://localhost:50598/ServiceTest.svc'. The content type text/html;
charset=utf-8 of the response message does not match the content type
of the binding (application/soap+xml; charset=utf-8). If using a
custom encoder, be sure that the IsContentTypeSupported method is
implemented properly. The first 1024 bytes of the response were: '?
Log in.
Usernamehttp://localhost:50598/ServiceTest.svc The HTML document does not
contain Web service discovery information.
I tried everything that I could find on the web. But couldn't find anything that would work.
My last consisted on fiddling with the web.config:
<system.serviceModel>
<bindings>
<webHttpBinding>
<binding name="UnsecuredBinding">
<security mode="None">
<transport clientCredentialType="None"/>
</security>
</binding>
</webHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="serviceBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="Management.ServiceAspNetAjaxBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
<services>
<service name="Management.ServiceTest" behaviorConfiguration="serviceBehavior">
<endpoint address="" behaviorConfiguration="Management.ServiceAspNetAjaxBehavior" binding="webHttpBinding" contract="ServiceLibrary.IService" bindingConfiguration="UnsecuredBinding"/>
</service>
</services>
</system.serviceModel>
I also added routes.IgnoreRoute to RouteConfig.cs
routes.IgnoreRoute("{resource}.svc/{*pathInfo}");
routes.IgnoreRoute("{resource}.svc");
and tried to add this to Global.asax
protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
HttpApplication context = (HttpApplication)sender;
if (context.Request.Url.ToString().Contains(".svc"))
{
context.Response.SuppressFormsAuthenticationRedirect = true;
}
}
My questions:
If I migrate the WCF methods to WebAPI, will the client need to do any modifications on their end?
If yes, how can I integrate WCF on my MVC/WebAPI project, without being affected by Log In barrier.
In answer to question 1, yes your clients would have to make modifications. WCF is SOAP/XML where as WebApi is REST/JSON(usually) for starters.
As for 2, if the WCF service is working fine, just leave it as it is. You don't need to include it in the project directly, simply use the "Add Service Reference" wizard in Visual Studio to make a client for interacting with it.
As a side note, the error you where experiancing was probably due to using the inbuilt IIS express instance with Visual Studio, which doesn't support running anonymous and authenticated applications at the same time, so your WCF service ended up with authentication enabled.

Where to configure a WCF service (REST and SOAP)

I am implementing a WCF Web Service responsible of publishing data via REST and SOAP by using multiple bindings. The service is to be hosted on an IIS.
I have previously written some WCF services and know a bit about configuring those by using the web.config and setting up routes in the Global.asax files, however I'm confused about how to make the most "clean" configuration or the best practice on configuring a WCF service.
Here's what I have figured so far:
The web.config can be used to setup bindings, endpoints, security etc - is this needed when hosting the service on IIS or can the configuration be done on the IIS?
By using the Global.asax we're able to configure routings (and lots of other stuff). but is it the right place to do this?
void Application_Start(object sender, EventArgs e)
{
RouteTable.Routes.Add(new ServiceRoute("Service", new WebServiceHostFactory(), typeof(Service)));
}
I've spent some time googling this topic and it seems that every link has it's own opinion on how to accomplish the task.
Therefore I would like some input on how to configure/implement a WCF service to support the following:
Publish the data via REST/JSON and
Pubish the data via SOAP/XML and publishing metadata
Provide to different services; http://domain.com/Service and http://domain.com/AuthService
For the record I'm aware of how to publish the data using both SOAP/REST - that's not the problem. I'm just trying to make to most clean/minimal configuration for the service.
Any feedback is greatly appreciated.
Here is how I've done this.
Web.config:
<system.serviceModel>
<services>
<service name="Service">
<endpoint address="soap" contract="IService" binding="basicHttpBinding"/>
<endpoint address="rest" contract="IService" binding="webHttpBinding" behaviorConfiguration="restBehavior"/>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="restBehavior">
<webHttp/>
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
The contract looks like this:
[ServiceContract]
public interface IService
{
[OperationContract]
[WebInvoke(UriTemplate="/Update/{id}", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
void Update(string id, Entity entity);
}
With .net 4.5 you can omit any configuration in .config file at all. [ServiceContract], [OperationContract], [DataContract] would be essential. They do not say that in documentation explicitly, but it works :)
".NET Framework 4.5 makes configuring a WCF service easier by removing the requirement for the service element. If you do not add a service section or add any endpoints in a service section and your service does not programmatically define any endpoints, then a set of default endpoints are automatically added to your service, one for each service base address and for each contract implemented by your service." - from
https://msdn.microsoft.com/en-us/library/ee358768%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

how to bind WCF service to IP address

I am developing a WCF service hosted by IIS, using VSTS2008 + C# + .Net 3.5. I find when reference the service from a client by using Add Service Reference..., client has to be able to resolve the machine name to IP address, because WSDL reference some schema file by machine name. Here is an example of a part of WSDL file, in order to parse WSDL file from client side to generate proxy, we have to be able to resolve machine name testmachine1 to related IP address,
<xsd:import schemaLocation="http://testmachine1/service.svc?xsd=xsd1"
namespace="http://schemas.microsoft.com/2003/10/Serialization/"/>
My question is, for some reason machine name cannot be resolved all the time (for non-technical reasons), so I want to bind to IP address of the hosting IIS server. Is it possible? If yes, appreciate if anyone could advise. Here is my current WCF web.config file, I want to know how to modify it to enable it to work with IP address,
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
<service behaviorConfiguration="Foo.WCF.ServiceBehavior"
name="Foo.WCF.CustomerManagement">
<endpoint address="" binding="basicHttpBinding"
contract="Foo.WCF.ICustomerManagement">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding"
contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="Foo.WCF.ServiceBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
thanks in advance,
George
If your WCF service is hosted in IIS, you cannot set a separate address. You must use the URL of the virtual directory where your SVC file lives - either with a machine name (http://yourserver/virtualdir/myservice.svc) or an IP (http://123.123.123.123/virtualdir/myservice.svc).
If you use the IP to add the service reference, that IP will be used in the WSDL generated by the service import.
If you host the WCF service yourself (Windows service, console app), you can set the service address in config, and use either machine name or IP for the machine.
Marc
I was having this same issue and seen your post while looking for answers to my own problem.
I think I may have found a solution, which was to change the IIS site binding to be that of the ip. I still don't understand why this can't be a setting in the .config file.
Here is the link to the solution that I found (http://blogs.msdn.com/wenlong/archive/2007/08/02/how-to-change-hostname-in-wsdl-of-an-iis-hosted-service.aspx).
Here is a link to my post on my issue (.NET WCF service references use server name rather than IP address causing issues when consuming).
Here is a link to my post about finding the solution (WCF (hosting service in IIS) - machine name automattically being picked up by WCF rather than IP?).

WCF Service - Backward compatibility issue

I'm just getting into creating some WCF services, but I have a requirement to make them backward compatible for legacy (.NET 1.1 and 2.0) client applications.
I've managed to get the services to run correctly for 3.0 and greater clients, but when I publish the services using a basicHttpBinding endpoint (which I believe is required for the compatibility I need), the service refactors my method signatures. e.g.
public bool MethodToReturnTrue(string seedValue);
appears to the client apps as
public void MethodToReturnTrue(string seedValue, out bool result, out bool MethodToReturnTrueResultSpecified);
I've tried every configuration parameter I can think of in the app.config for my self-hosting console app, but I can't seem to make this function as expected. I suppose this might lead to the fact that my expectations are flawed, but I'd be surprised that a WCF service is incapable of handling a bool return type to a down-level client.
My current app.config looks like this.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
<service behaviorConfiguration="MyServiceTypeBehaviors" Name="MyCompany.Services.CentreService.CentreService">
<clear />
<endpoint address="http://localhost:8080/CSMEX" binding="basicHttpBinding" bindingConfiguration="" contract="IMetadataExchange" />
<endpoint address="http://localhost:8080/CentreService" binding="basicHttpBinding" bindingName="Compatible" name="basicEndpoint" contract="MyCompany.Services.CentreService.ICentreService" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="MyServiceTypeBehaviors" >
<serviceMetadata httpGetEnabled="true" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
Can anyone advise, please?
Ah, this is killing me! I did this at work about 3 months ago, and now I can't remember all the details.
I do remember, however, that you need basicHttpBinding, and you can't use the new serializer (which is the default); you have to use the "old" XmlSerializer.
Unfortunately, I don't work at the place where I did this anymore, so I can't go look at the code. I'll call my boss and see what I can dig up.
OK, we needed to resolve this issue in the short term, and so we came up with the idea of a "interop", or compatibility layer.
Baiscally, all we did was added a traditional ASMX web service to the project, and called the WCF service from that using native WCF calls. We were then able to return the appropriate types back to the client applications without a significant amount of re-factoring work. I know it was a hacky solution, but it was the best option we had with such a large legacy code-base. And the added bonus is that it actually works surprisingly well. :)
You do have to use the XmlSerializer. For example:
[ServiceContract(Namespace="CentreServiceNamespace")]
[XmlSerializerFormat(Style=OperationFormatStyle.Document, SupportFaults=true, Use=OperationFormatUse.Literal)]
public interface ICentreService {
[OperationContract(Action="CentreServiceNamespace/MethodToReturnTrue")]
bool MethodToReturnTrue(string seedValue);
}
You have to manually set the operation action name because the auto-generated WCF name is constructed differently from the ASMX action name (WCF includes the interface name as well, ASMX does not).
Any data contracts you use should be decorated with [XmlType] rather than [DataContract].
Your config file should not need to change.

Categories