how can I add a JSONP endpoing for WCF Ria Services to enable cross-domain calls? - jsonp

I'm aware that WCF RIA Services has a Microsoft.ServiceModel.DomainServices.Hosting.JsonEndpointFactory that I can use to enable JSON.
I need to enable cross-domain calls via JSONP. Is there an existing DomainServiceEndpointFactory that will accomplish this?

I just needed to do this - I overrode JsonEndpointFactory and tinkered with the binding in there, then added an endpoint using the new class.
namespace Bodge
{
public class JsonPEndpointFactory : JsonEndpointFactory
{
public override IEnumerable<ServiceEndpoint> CreateEndpoints(DomainServiceDescription description, DomainServiceHost serviceHost)
{
IEnumerable<ServiceEndpoint> endPoints = base.CreateEndpoints(description, serviceHost);
foreach (ServiceEndpoint endPoint in endPoints)
{
if (endPoint.Binding is WebHttpBinding)
{
((WebHttpBinding)endPoint.Binding).CrossDomainScriptAccessEnabled = true;
}
}
return endPoints;
}
}
}
<endpoints>
<add name="JSONP" type="Bodge.JsonPEndpointFactory, Bodge, Version=1.0.0.0"/>
</endpoints>
Then access your service with the endpoint and the callback query param e.g.
http://blah/service.svc/JSONP/GetStuff?callback=callbackname
Hope that helps,
Chris.

Formatting in comments isn't great, so for future reference here are the required usings and assemblies.
Thanks very much, that's exactly what I needed!for future reference, these are the using statements:
Namespaces:
using System.Web;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.ServiceModel.DomainServices.Hosting;
using System.ServiceModel.DomainServices.Server;
using Microsoft.ServiceModel.DomainServices.Hosting;
Assemblies
NETFX 4.0
System.ServiceModel
System.ServiceModel.Web
WCF RIA Services V1.0 SP2 RC
System.ServiceModel.DomainServices.Hosting
System.ServiceModel.DomainServices.Server
WCF RIA Services Toolkit (September 2011)
Microsoft.ServiceModel.DomainServices.Hosting

Related

Exception "Specified method is not supported" when call method from WCF web service with Net Core 3.1

I'm trying to connect a Web service SOAP12 using WCF and .NET Core 3.1.
I've generate the Reference.cs using WCF connected service from Visual Studio 2019 (v16.6.2). VS2019 generates the file but shows some warnings.
I tried build a custom binding but always throw same error.
var customBinding = new CustomBinding(encodingBindingElement)
{
Elements = { new HttpTransportBindingElement //or HttpsTransportBindingElement for https:
{
MaxBufferSize = int.MaxValue,
MaxReceivedMessageSize = int.MaxValue,
}}
};
Already tried WSHttpBinding.
var binding = new WSHttpBinding(SecurityMode.TransportWithMessageCredential)
{
MaxBufferPoolSize = int.MaxValue,
MaxReceivedMessageSize = int.MaxValue,
};
has anyone managed to consume WCF SOAP12 with .NET Core 3.1 that can help me?
This is a message security issue that is not yet supported by the .Net Core framework. This usually happens when the binding of the service endpoint contains elements (functions) that are not supported in .NET Core, such as certain security elements (message security). .net core 3.1 does not yet support SymmetricSecurityBindingElement.
.Net core only supports the following bindings:

Xamarin.Forms Add Connected Service on WCF only generated async method

I just begun to do Xamarin.Forms with .Net Standard 2.0 (PCL) project. I'm trying to consume my WCF web service but never got it successfully done.
I have created a simple WCF as below
[ServiceContract]
public interface IWcfConnection
{
[OperationContract]
string GetHelloWorld();
}
the implementation as below
public class WcfConnection : IWcfConnection
{
public string GetHelloWorld()
{
return "Hello World";
}
}
It's a very simple WCF, when I go to my Xamarin.Forms and right click on the "Connected Service", there is no "Add Web Service", but only "Add Connected Service", so I selected that as below
Then select "Microsoft WCF Web Service Service Provider"
Select the option as below (I untick everything because if I add more than 1 service, it will crash)
When I look into the reference.cs created, there is only async method created.
public System.Threading.Tasks.Task<string> GetHelloWorldAsync()
{
return base.Channel.GetHelloWorldAsync();
}
1) May I know why only async is created? Is it for .net standard and core, only async services will be created? As I read somewhere.
2) If so, how do I consume the web service?
In my xaml.cs file, I did the following,
WcfConnectionService.WcfConnectionClient client = new WcfConnectionService.WcfConnectionClient(new WcfConnectionService.WcfConnectionClient.EndpointConfiguration());
string abc = client.GetHelloWorldAsync().GetAwaiter().GetResult();
But I'm getting error and unable to work accordingly. Anybody got any idea?
Unhandled Exception:
System.ServiceModel.FaultException`1[[System.ServiceModel.ExceptionDetail, System.ServiceModel, Version=2.0.5.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: Error in deserializing body of request message for operation 'GetHelloWorld'. OperationFormatter encountered an invalid Message body. Expected to find node type 'Element' with name 'GetHelloWorld' and namespace 'http://tempuri.org/'. Found node type 'Element' with name 'GetHelloWorldAsync' and namespace 'http://tempuri.org/'
At the moment Xamarin apps aren't compatible with the Task-based asynchronous WCF proxy methods that the WCF Web Service Reference connected service provider generates for .NET Standard projects (bugzilla.xamarin.com Bug 51959).
Generate an older compatible style of WCF proxy methods via checked "Generate Synchronous Operations" checkbox on Configure WCF Web Service Reference screen:
Consume the web service:
KimlikServiceReference.KPSPublicSoapClient soapClient = new KimlikServiceReference.KPSPublicSoapClient(KimlikServiceReference.KPSPublicSoapClient.EndpointConfiguration.KPSPublicSoap);
//KimlikServiceReference.TCKimlikNoDogrulaResponse response = soapClient.TCKimlikNoDogrulaAsync(TCKimlikNo, Ad, Soyad, DogumYili).Result;
bool result = soapClient.TCKimlikNoDogrula(TCKimlikNo, Ad, Soyad, DogumYili);

Hosting WCF service on linux

Is there Any way of hosting WCF service on Linux.
I read about wine but i didn't see any example of hosting WCF service with it.
P.S : I have tried mono and mod_mono but to no avail.
You can host it in a stand-alone console application like so:
using System;
using System.ServiceModel;
using Service;
namespace Host
{
class MainClass
{
public static void Main (string[] args)
{
Console.WriteLine ("WCF Host!");
var binding = new BasicHttpBinding ();
var address = new Uri ("http://localhost:8080");
var host = new ServiceHost (typeof(GreeterWcfService));
host.AddServiceEndpoint (
typeof(IGreeterWcfService), binding, address);
host.Open ();
Console.WriteLine ("Type [Enter] to stop...");
Console.ReadLine ();
host.Close ();
}
}
}
Where GreeterWcfService is the WCF service class itself and IGreeterWcfService is the service contract.
Full working example solution in GitHub - with separate projects for the service, the hosting and a client. Check it out.
Its possible but you should refer to this link for understanding current state and known issues - http://www.mono-project.com/docs/web/wcf/. It's limited now. For eg. if you wish to use WSHttpBinding its not supported currently.

Can we use ChannelFactory<T> when the contracts (.NET classes, interfaces) are NOT defined in some common library consumed by client and service?

I was exploring ChannelFactory and while doing so I did following:
A service contract in assembly named "Common":
namespace Common
{
using System.ServiceModel;
[ServiceContract(Name = "ITestService", Namespace = "http://test/")]
public interface ITestService
{
[OperationContract(Name = "SayHello")]
string SayHello(string request);
}
}
A service hosted under web application called "WcfServiceApp":
Note that I have created another service interface (contract) to create a service. But the names of contracts and the namespaces are same as the contract defined in the "Common" assembly.
namespace WcfServiceApp
{
[ServiceContract(Name = "ITestService", Namespace = "http://test/")]
public interface ITestServiceWithDiffDotNetName
{
[OperationContract(Name = "SayHello")]
string SayHelloAgain(string name);
}
// This service implements new service contract.
public class TestService : ITestServiceWithDiffDotNetName
{
public string SayHelloAgain(string request)
{
return "hello " + request;
}
}
// This service implements common service contract
public class TestService2 : Common.ITestService
{
public string SayHello(string request)
{
return "hello " + request;
}
}
}
There are two ".svc" files (TestService.svc and TestService2.svc), each for services created above. Web.config has following:
<system.serviceModel>
<services>
<service name ="WcfServiceApp.TestService" >
<endpoint binding="basicHttpBinding" contract="WcfServiceApp.ITestServiceWithDiffDotNetName"></endpoint>
</service>
<service name ="WcfServiceApp.TestService2" >
<endpoint binding="basicHttpBinding" contract="Common.ITestService"></endpoint>
</service>
</services>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
A client which calls these two services:
Note that client is using ChannelFactory and the service contract defined in "Common" library.
ChannelFactory<ITestService> commonServiceChannel = new ChannelFactory<ITestService>(new BasicHttpBinding(), "http://localhost/WcfServiceApp/TestService.svc");
var proxy = commonServiceChannel.CreateChannel();
var response = proxy.SayHello("Mike"); // response = "Hello"
ChannelFactory<ITestService> commonServiceChannel2 = new ChannelFactory<ITestService>(new BasicHttpBinding(), "http://localhost/WcfServiceApp/TestService2.svc");
var proxy2 = commonServiceChannel2.CreateChannel();
var response2 = proxy2.SayHello("Mike"); // response2 = "Hello Mike"
Question:
I observed that first service (created using some different service contract) receives the null argument whereas argument received in second service (created using service contract used to create ChannelFactory) is "Mike" as expected.
In Fiddler, I can see that request parameter correctly.
Why does this happen?
If all XML names and namespaces are same (although names of .NET interfaces are different), should the service call not succeed as underlying SOAP messages would be same?
I am afraid what will happen if my customer's applications want to create service in Java and my application is supposed to call it?
Try this, get the WSDL document from each version of your service. Compare the WSDL documents (.NET 4.5 supports single file WSDL documents out of the box) to see what WCF is expecting in the soap message for each service. Chances are a default XML namespace was taken from the .NET (different) namespaces somewhere thus making the "identical" service contracts actually be different. WCF does a lot for you in naming the XML namespaces and you will likely need to manually override those defaults throughout the service, operation and data contracts to make both services support identical soap messages.
On integrating with Java, as long as the Java client can be generated from the WSDL the service outputs there's a chance there won't be any issues. The big exception is configuring the security and authentication aspects of the Java client. This good blog post on specific WCF bindings for Java interop would be worth a look when working with Java based clients.
Thanks to Sixto Saez first.
I compared the WSDL files generated (using svcutil.exe) by both the services and found that they were NOT EXACTLY SAME. However, it was not due to any conflict in the name or namespace of either ServiceContract or OperationContract itself!
It was due to difference in the parameter names used in the definition of the OperationContracts!
You can see that OperationContract "SayHello" has a parameter named "request". On the other hand, OperationContract named "SayHelloAgain" has a parameter named "name". When I changed the name of the parameter from "request" to "name" as it is in second OperationContract, it worked!
So the conclusion is:
ChannelFactory WORKS when the contracts (.NET classes, interfaces) are NOT defined in some common library consumed by client and service. Only thing is that WSDLs generated by those service contracts have to match with each other.

Problem creating WCF service with Windsor

Does anyone know how to configure WCF using Windsor on IIS 7.0? I'm using the latest from WCF Windsor facility trunk and Windsor 2.1.1. The example on http://www.castleproject.org/container/facilities/trunk/wcf/index.html is out of date. Even demo project in WCF facility doesn't mention how to setup WCF service in IIS using the config and I couldn't find any example where I can setup WCF on server side using system.serviceModel section of web.config or even through code. When I use the following code it always creates basicHttpBinding and I couldn't figure out how to setup different bindings.
protected void Application_Start(object sender, EventArgs e)
{
var returnFaults = new ServiceDebugBehavior
{
IncludeExceptionDetailInFaults = true,
HttpHelpPageEnabled = true
};
var metadata = new ServiceMetadataBehavior {HttpGetEnabled = true};
container = new WindsorContainer()
.AddFacility<WcfFacility>()
.Register(
Component.For<IServiceBehavior>().Instance(returnFaults),
Component.For<IServiceBehavior>().Instance(metadata),
Component.For<IMyService>()
.Named("MyService")
.ImplementedBy<MyService>()
.LifeStyle.Transient
);
}
And here is MyService.svc
<%# ServiceHost Language="C#" Debug="true" Service="MyService"
Factory="Castle.Facilities.WcfIntegration.DefaultServiceHostFactory, Castle.Facilities.WcfIntegration" %>
I recently wrote a blog post about Windsor's WCF Facility. Be sure to read the comments as well, as they include a discussion involving one of Windsor's active committers; they should give you a pretty good impression of the future direction.