Creating REST and SOAP endpoints for a WCF service? - wcf

I need a service with two endpoints.When I create service with 1 endpoint point, it works fine but when I add 2 endpoints it complains endpoint not found.
Endpoint without address extension works fine but not with address ('download')
Here is my configuration:
Server
<serviceBehaviors>
<behavior name="MessageStreamBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true" httpHelpPageEnabled="true" />
<dataContractSerializer maxItemsInObjectGraph="2147483647"/>
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<basicHttpBinding>
<!--stream for video upload-->
<binding name="httpLargeMessageStream"
maxReceivedMessageSize="4294967295"
openTimeout="00:01:00" closeTimeout="00:30:00" sendTimeout="00:30:00"
transferMode="Streamed">
<security mode="Transport" />
</binding>
</basicHttpBinding>
<webHttpBinding>
<!--stream for video download-->
<binding name="webHttpLargeMessageStream"
maxReceivedMessageSize="4294967295"
openTimeout="00:01:00" closeTimeout="00:30:00" sendTimeout="00:30:00"
transferMode="Streamed">
<security mode="Transport" />
</binding>
</webHttpBinding>
</bindings>
<services>
<!--service for streamservice; video upload and download-->
<service name="Trigger.CSSD.Service.Services.MediaService" behaviorConfiguration="MessageStreamBehavior" >
<endpoint name="restDownloadHttpStream"
behaviorConfiguration="webHttpBehavior" address="download"
binding="webHttpBinding" bindingConfiguration="webHttpLargeMessageStream"
contract="Trigger.CSSD.Service.Services.IDownloadMediaService" />
<endpoint name="uploadHttpStream"
binding="basicHttpBinding" bindingConfiguration="httpLargeMessageStream"
contract="Trigger.CSSD.Service.Services.IUploadMediaService" />
</service>
</services>
Client
<endpoint address="https://cssd-services:44300/MediaService.svc"
binding="basicHttpBinding" bindingConfiguration="basicHttpStream"
contract="MediaService.IUploadMediaService" name="basicHttpStream" />
<endpoint address="https://cssd-services:44300/MediaService.svc/download"
binding="webHttpBinding" bindingConfiguration="webHttpStream" behaviorConfiguration="webHttpBehavior"
contract="MediaService.IDownloadMediaService" name="webHttpStream" />
</client>
SERVICE
[ServiceContract]
public interface IDownloadMediaService
{
[OperationContract]
// [ContentType("video/mp4")]
// [WebGet(UriTemplate = "media/{name}")]
Stream GetMedia(String name);
}
[ServiceContract]
public interface IUploadMediaService
{
//http://msdn.microsoft.com/en-us/library/ms751463.aspx
[OperationContract]
FileUploadResponseMessage UploadFile(FileUploadMessage message);
}
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
public class MediaService : IDownloadMediaService, IUploadMediaService
{......
}
ANy suggestion? Thanks.

Related

How to add Custom Service Behavior To WCF Configuration

I have a service with ComplexType in the Uri Template Parameter, I have Override the CanConvert() and ConvertStringToValue() methods to the class MyQueryStringConverter.
I need to add that behavior to the web.config file.
Here is the Behavior
public class MyWebHttpBehavior : WebHttpBehavior
{
protected override QueryStringConverter GetQueryStringConverter(OperationDescription operationDescription)
{
return new MyQueryStringConverter();
}
}
Here Is the Configuration File :
<system.serviceModel>
<bindings>
<webHttpBinding>
<binding name="webHttp"
maxReceivedMessageSize="20000000" >
<security mode="None">
<transport clientCredentialType = "None"/>
</security>
</binding>
</webHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="mexBehaviour">
<serviceMetadata httpGetEnabled="true"/>
<useRequestHeadersForMetadataAddress>
<defaultPorts>
<add scheme="http" port="80" />
</defaultPorts>
</useRequestHeadersForMetadataAddress>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<service name="Service.Service1" behaviorConfiguration="mexBehaviour">
<endpoint address="" binding="webHttpBinding" contract="Service.IService1" bindingConfiguration="webHttp" behaviorConfiguration="web"/>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange">
</endpoint>
</service>
</services>
</system.serviceModel>
Please help how to add that behavior.
First, you must register your behavior, using behaviorExtensions element in your configuration file:
<system.serviceModel>
<extensions>
<behaviorExtensions>
<add name="MyWebHttpBehavior"
type="FullNameSpace.MyWebHttpBehavior, MyWebHttpBehavior.AssemblyName, Version=1.0.0.0, Culture=neutral" />
</behaviorExtensions>
</extensions>
Where:
"FullNameSpace.MyWebHttpBehavior" is your class with full namespace,
"MyWebHttpBehavior.AssemblyName" is your assembly name with extension
where your class is compiled.
"1.0.0.0" is the version of your assembly
"neutral" is your Culture. Change if any special Culture is required
Secound, declare your behavior:
<behaviors>
<endpointBehaviors>
<behavior name="customMyWebHttpBehavior">
<webHttp/>
<MyWebHttpBehavior/>
</behavior>
</endpointBehaviors>
<behaviors>
Next, add to the binding:
<bindings>
<webHttpBinding>
<binding name="webHttp"
maxReceivedMessageSize="20000000" >
<security mode="None">
<transport clientCredentialType = "None"/>
</security>
</binding>
<MyWebHttpBehavior />
</webHttpBinding>
</bindings>
And finally, set the behavior to your service endpoint:
<endpoint address="" binding="webHttpBinding" contract="Service.IService1" bindingConfiguration="webHttp" behaviorConfiguration="customMyWebHttpBehavior"/>
I've copied this configuration of a service I've developed and changed the names to fit your class/configuration, but check if I did not wrote anything wrong.
I've used this link as base to setup my configuration: Custom Behavior won't register in my web.config
Hope it helps.

WCF configuration to HTTPS with SSL not working

I am trying to use an HTTPS service using WCF but it keeps returning all kinds of errors. When I use the basicHttpBinding to call the HTTP URL it works fine but when I swich to webHttpBinding or wsHttpBinding and call the HTTPS URL I'm getting these errors:
The content type text/xml;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.
And here is my configuration:
<system.serviceModel>
<services>
<service behaviorConfiguration="ServiceBehavior" name="MentorGraphicsIM">
<endpoint name="MentorGraphicsServiceEndPoint"
address=""
binding="webHttpBinding"
bindingConfiguration="webHttpPadsTouch"
behaviorConfiguration="ServiceEndPointBehavior"
contract="IMentorGraphics">
</endpoint>
</service>
</services>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding" />
</basicHttpBinding>
<webHttpBinding>
<binding name="webHttpPadsTouch">
</binding>
</webHttpBinding>
<wsHttpBinding>
<binding name="WsHttpBinding">
<security mode="Transport" />
</binding>
</wsHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="false"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<!--Required default endpoint behavior when using webHttpBinding-->
<endpointBehaviors>
<behavior name="ServiceEndPointBehavior">
<webHttp />
</behavior>
<behavior name="ClientEndPointBehavior">
<webHttp defaultBodyStyle="Wrapped" defaultOutgoingResponseFormat="Xml" helpEnabled="true" />
</behavior>
</endpointBehaviors>
</behaviors>
<client>
<endpoint name="PadsTouch_HTTP"
address="https://XXX"
binding="wsHttpBinding"
bindingConfiguration="WsHttpBinding"
contract="IEntitle" />
</client>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
My Interface is:
[ServiceContract(Namespace = "xxx", ProtectionLevel = ProtectionLevel.None)]
public interface IMentorGraphics
{
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "MyMethod", RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml)]
ServiceResponse MyMethod(MyParams #param);
}
replace service binding from bindingConfiguration="webHttpPadsTouch" to bindingConfiguration="WsHttpBinding"

Duplication of EndPoint but i cannot see it

I have a web service.
This is (part of my) my web config:
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="LicensedBehaviour">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
<behavior name="NetTCPBehaviour">
<serviceTimeouts transactionTimeout="0.00:00:30" />
<serviceDebug includeExceptionDetailInFaults="false" />
<dataContractSerializer maxItemsInObjectGraph="65536" />
<serviceMetadata httpGetEnabled="true" />
</behavior>
<behavior name="">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="License" behaviorConfiguration="LicensedBehaviour">
<endpoint address="License.svc" binding="basicHttpBinding" bindingConfiguration="NormalHttpBindingEndPoint" contract="ILicense" name="wsLicense" />
</service>
<service name="testme" behaviorConfiguration="NetTCPBehaviour">
<endpoint address="net.tcp://localhost:808/Sync2.svc" binding="netTcpBinding" contract="ISync2" name="wsMotionUploader" bindingConfiguration="NetTCPBindingEndPoint"/>
</service>
</services>
<bindings>
<basicHttpBinding>
<binding name="NormalHttpBindingEndPoint" closeTimeout="00:02:00" openTimeout="00:02:00">
<readerQuotas maxArrayLength="32768" maxStringContentLength="2147483647" />
</binding>
</basicHttpBinding>
<netTcpBinding>
<binding name="NetTCPBindingEndPoint" receiveTimeout="00:15:00" sendTimeout="00:15:00" transferMode="Streamed" closeTimeout="00:02:00" openTimeout="00:02:00"
maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" maxBufferSize="2147483647">
<readerQuotas maxArrayLength="32768" />
<security mode="None">
<transport clientCredentialType="None" protectionLevel="None" />
<message clientCredentialType="None" />
</security>
</binding>
</netTcpBinding>
</bindings>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
This is my service:
public void DoWork(Stream image)
{
var img = System.Drawing.Bitmap.FromStream(image);
}
This is my interface:
[OperationContract(IsOneWay = true)]
void DoWork(Stream image);
This is my client:
wsSyncFastest.Sync2Client client = new wsSyncFastest.Sync2Client();
using (Bitmap bmp = new Bitmap("d:\\bf.jpg"))
{
using (MemoryStream ms = new MemoryStream())
{
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
client.DoWork(ms);
ms.Close();
}
}
This is my error message:
An endpoint configuration section for contract 'wsSyncFastest.ISync2' could not be loaded because more than one endpoint configuration for that contract was found. Please indicate the preferred endpoint configuration section by name.
this is my stack Error:
at System.ServiceModel.Description.ConfigLoader.LookupChannel(ContextInformation configurationContext, String configurationName, ContractDescription contract, EndpointAddress address, Boolean wildcard, Boolean useChannelElementKind, ServiceEndpoint& serviceEndpoint)
at System.ServiceModel.Description.ConfigLoader.LookupEndpoint(String configurationName, EndpointAddress address, ContractDescription contract, ContextInformation configurationContext)
at System.ServiceModel.Description.ConfigLoader.LookupEndpoint(String configurationName, EndpointAddress address, ContractDescription contract)
at System.ServiceModel.ChannelFactory.InitializeEndpoint(String configurationName, EndpointAddress address)
at System.ServiceModel.ChannelFactory1..ctor(String endpointConfigurationName, EndpointAddress remoteAddress)
at System.ServiceModel.ChannelFactory1..ctor(String endpointConfigurationName)
at System.ServiceModel.ConfigurationEndpointTrait1.CreateSimplexFactory()
at System.ServiceModel.ConfigurationEndpointTrait1.CreateChannelFactory()
at System.ServiceModel.ClientBase1.CreateChannelFactoryRef(EndpointTrait1 endpointTrait)
at System.ServiceModel.ClientBase1.InitializeChannelFactoryRef()
at System.ServiceModel.ClientBase1..ctor()
at LiteEdition.wsSyncFastest.Sync2Client..ctor() in g:\dev20140604\LiteEdition\LiteEdition\Service References\wsSyncFastest\Reference.cs:line 51
at LiteEdition.StartUp.button1_Click(Object sender, EventArgs e) in g:\dev20140604\LiteEdition\LiteEdition\StartUp.cs:line 2646
I cannot see any probelms but obviously there is.
Can anyone assist?
ADDITIONAL:
My client app.config:
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="wsLicense" />
<binding name="BasicHttpBinding_ISync2" />
</basicHttpBinding>
<netTcpBinding>
<binding name="NetTcpBinding_ISync2" />
</netTcpBinding>
</bindings>
<client>
<endpoint address="http://aurl/License.svc/License.svc"
binding="basicHttpBinding" bindingConfiguration="wsLicense"
contract="wsLicense.ILicense" name="wsLicense" />
<endpoint address="http://www.informedmotion.co.uk/Sync2.svc"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_ISync2"
contract="wsSyncFastest.ISync2" name="BasicHttpBinding_ISync2" />
<endpoint address="net.tcp://dsvr019492/Sync2.svc" binding="netTcpBinding"
bindingConfiguration="NetTcpBinding_ISync2" contract="wsSyncFastest.ISync2"
name="NetTcpBinding_ISync2">
<identity>
<servicePrincipalName value="host/DSVR019492" />
</identity>
</endpoint>
</client>
</system.serviceModel>
Try passing the name of the endpoint in when you create the proxy object and see if that fixes it.
If you intend on using the basicHttpBinding, it will be:
wsSyncFastest.Sync2Client client = new wsSyncFastest.Sync2Client("BasicHttpBinding_ISync2");
If you intend on using the netTcpBinding it will be:
wsSyncFastest.Sync2Client client = new wsSyncFastest.Sync2Client("NetTcpBinding_ISync2");

DotNetOpenAuth and ResourceServer service https configuration

I am trying to configure the DataApi.svc service of the DotNetOpenAuth to call my resources via https using AJAX.
I can call the service and hit the code behind but the OperationContext.Current.ServiceSecurityContext will be not authenticated
In IIS, I have "Anonymous authentication" set to "true".
In Fiddler I can see that the header is sent:
Authorization: Bearer gAAAAMcRmG5vw3LykShq7cNOEGUACBiNtlVGxGYdSVfkkXjR-[truncated]
The interface is decorated like that:
[ServiceContract]
public interface IDataApi {
[OperationContract, WebGet(UriTemplate = "/email", ResponseFormat = WebMessageFormat.Json)]
string GetEmail();
And here is my config:
<bindings>
<wsHttpBinding>
<binding>
<security mode="Transport">
<transport clientCredentialType="None"/>
</security>
</binding>
</wsHttpBinding>
<webHttpBinding>
<binding>
<security mode="Transport">
<transport clientCredentialType="None"/>
</security>
</binding>
</webHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="DataApiBehavior">
<serviceDebug includeExceptionDetailInFaults="true" />
<serviceMetadata httpsGetEnabled="true" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="DataApiWebBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="DataApiBehavior" name="OAuthResourceServer.DataApi">
<endpoint address="" binding="wsHttpBinding" contract="OAuthResourceServer.Code.IDataApi" />
<endpoint address="mex" binding="mexHttpsBinding" contract="IMetadataExchange" />
<endpoint address="web" binding="webHttpBinding" contract="OAuthResourceServer.Code.IDataApi" behaviorConfiguration="DataApiWebBehavior">
</endpoint>
</service>
</services>
Any idea of what can be wrong?
Thanks!
I was missing the
<serviceAuthorizationserviceAuthorizationManagerType="OAuthResourceServer.Code.OAuthAuthorizationManager, OAuthResourceServer" principalPermissionMode="Custom" />
in the service behavior! Solved :)

Endpoint behavior configuration WCF with Protobuf-net

I have a WCF service (.NET 4) that exposes 4 endpoints of which one endpoint is configured with protobuf-net (V1.0.0.280) behavior extension. However, I noticed that protobuf-net behavior kicks in for ALL defined endpoints including the ones protbuf-net is not configured for! I have pasted my config below. Am I missing something? Any help is greatly appreciated .. thx
<service name="MyService" behaviorConfiguration="MyServiceBehavior">
<endpoint address="Http.Basic" binding="basicHttpBinding" bindingConfiguration="Http.Basic.Config" contract="IMyService" behaviorConfiguration="DefaultBehavior" />
<endpoint address="Http.Binary" binding="customBinding" bindingConfiguration="Http.Binary.Config" contract="IMyService" behaviorConfiguration="DefaultBehavior" />
<endpoint address="Tcp.Binary" binding="customBinding" bindingConfiguration="Tcp.Binary.Config" contract="IMyService" behaviorConfiguration="DefaultBehavior" />
<endpoint address="Http.ProtoBuf" binding="basicHttpBinding" bindingConfiguration="Http.Basic.Config" contract="IMyService" behaviorConfiguration="ProtoBufBehavior" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8085/MyService"/>
<add baseAddress="net.tcp://localhost:8086/MyService"/>
</baseAddresses>
</host>
</service>
<behaviors>
<serviceBehaviors>
<behavior name="MyServiceBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="DefaultBehavior">
<dataContractSerializer maxItemsInObjectGraph="2147483647" />
</behavior>
<behavior name="ProtoBufBehavior">
<ProtoBufSerialization />
</behavior>
</endpointBehaviors>
</behaviors>
<bindings>
<basicHttpBinding>
<binding name="Http.Basic.Config" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" />
<security mode="None">
<transport clientCredentialType="None" proxyCredentialType="None" realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
</basicHttpBinding>
<customBinding>
<binding name="Http.Binary.Config" closeTimeout="00:10:00" openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00">
<binaryMessageEncoding />
<httpTransport allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" />
</binding>
<binding name="Tcp.Binary.Config" closeTimeout="00:10:00" openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00">
<binaryMessageEncoding />
<tcpTransport hostNameComparisonMode="StrongWildcard" />
</binding>
</customBinding>
</bindings>
That is odd, but (checks the code) I only ever apply changes within an endpoint supplied to me by WCF:
void IEndpointBehavior.ApplyClientBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.ClientRuntime clientRuntime)
{
ReplaceDataContractSerializerOperationBehavior(endpoint);
}
void IEndpointBehavior.ApplyDispatchBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.EndpointDispatcher endpointDispatcher)
{
ReplaceDataContractSerializerOperationBehavior(endpoint);
}
private static void ReplaceDataContractSerializerOperationBehavior(ServiceEndpoint serviceEndpoint)
{
foreach (OperationDescription operationDescription in serviceEndpoint.Contract.Operations)
{
ReplaceDataContractSerializerOperationBehavior(operationDescription);
}
}
private static void ReplaceDataContractSerializerOperationBehavior(OperationDescription description)
{
DataContractSerializerOperationBehavior dcsOperationBehavior = description.Behaviors.Find<DataContractSerializerOperationBehavior>();
if (dcsOperationBehavior != null)
{
description.Behaviors.Remove(dcsOperationBehavior);
description.Behaviors.Add(new ProtoOperationBehavior(description));
}
}
i.e. "given an endpoint (by WCF), loop over each operation (method) in that endpoint, and change the serializer from DCS to PB"
This raises the intriguing possibility that the contract definitions (and hence the operation definitions) are themselves shared between all the endpoints - but I am honestly not sure about that. If that is the case, I don't see that it would ever be possible to have different processors per endpoint. I am not, however, a WCF-guru. This is... perplexing.