Silverlight WCF web service hosting on domain.com and www.domain.com - asp.net

There's so much stuff on this online but no one seems to be able to answer this... Hopefully someone here will be!
So i have a WCF web service hosted at godaddy.com. Everything works great when i try accessing it using:
http://**www.**domain.com/DataService.svc
problem is when i remove the www i.e.
http://domain.com/DataService.svc
Here's my web.config servicemodel section:
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="DataServiceBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="DataServiceBehavior"
name="DataService">
<endpoint address="" binding="basicHttpBinding" contract="IDataService"/>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
Here's my ServiceReferences.ClientConfig
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IDataService" maxBufferSize="2147483647"
maxReceivedMessageSize="2147483647">
<security mode="None" />
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://www.domain.ca/DataService.svc" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IDataService" contract="Web.IDataService"
name="BasicHttpBinding_IDataService" />
</client>
</system.serviceModel>
My service is using a CustomServiceFactory
public class CustomServiceHostFactory : ServiceHostFactory
{
/// <summary>
/// A custom method to eliminate multiple base addresses from the IIS host creation process
/// </summary>
/// <param name="serviceType">The service type to be created</param>
/// <param name="baseAddresses">A list of the base addresses</param>
/// <returns>A service host</returns>
protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
{
if (baseAddresses.Length > 1)
{
ServiceHost customServiceHost =
new ServiceHost(serviceType, baseAddresses[1]);
return customServiceHost;
}
return new ServiceHost(serviceType, baseAddresses[0]);
}
}
Basically what i would like is my WCF webservice to be reachable whether the user enters domain.com or www.domain.com into his (or her) browser.
Any help will be greatly appreciated!
ps. Running IIS 7 with ASP 3.5
Thnaks!
Simon

You're running across a cross domain issue. See here for a list of reasons as to why it happens. As it turns out "www" and no-www are different domains even though they don't seem like it. You'll need to add a cross domain file. See Tim's blog for good info

Related

the resource cannot be found wcf service with SSL

I created a REST api using asp.net vb and I was trying to invoke the api through secure connection (https) but I had an error
The resource cannot be found
I can invoke any method using (http), but with (https) I can't. And I can access the main page of api (service.svc) using the (https) but the problem with functions!! below are my config and function header.
<system.serviceModel>
<services>
<service name="RESTAPI" behaviorConfiguration="MyServiceTypeBehaviors">
<endpoint address="customBinding" binding="customBinding" bindingConfiguration="basicConfig" contract="RESTAPI"/>
<endpoint address="" behaviorConfiguration="HerbalAPIAspNetAjaxBehavior"
binding="webHttpBinding" contract="HerbalAPI" />
<endpoint contract="RESTAPI" binding="mexHttpBinding" address="mex" />
</service>
</services>
<!-- **** Services ****-->
<behaviors>
<serviceBehaviors>
<behavior name="MyServiceTypeBehaviors">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="HerbalAPIAspNetAjaxBehavior">
<webHttp helpEnabled="true" />
</behavior>
</endpointBehaviors>
</behaviors>
<bindings>
<customBinding>
<binding name="basicConfig">
<binaryMessageEncoding/>
<httpTransport transferMode="Streamed" maxReceivedMessageSize="67108864"/>
</binding>
</customBinding>
</bindings>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"
multipleSiteBindingsEnabled="true" />
API Class
<ServiceContract(Namespace:="")>
<AspNetCompatibilityRequirements(RequirementsMode:=AspNetCompatibilityRequirementsMode.Allowed)>
Public Class RESTAPI
<OperationContract()>
<WebInvoke(Method:="GET", ResponseFormat:=WebMessageFormat.Json, RequestFormat:=WebMessageFormat.Json)>
Public Function test(ByVal st As String) As JSONResultString
//any code
End Function
End Class
You need to define a special binding configuration in your web.config file to allow the SVC service to bind correctly for HTTPS requests.
Please have a look at this blog post: https://weblogs.asp.net/srkirkland/wcf-bindings-needed-for-https
Your service will already be defined in the web.config, just add the bindingConfiguration attribute:
<services>
<service name="TestService">
<endpoint address="" behaviorConfiguration="TestServiceAspNetAjaxBehavior"
binding="webHttpBinding" bindingConfiguration="webBindingHttps" contract="TestService" />
</service>
</services>
Then define the special binding settings for the webHttpBinding as so, the magic part that fixes the HTTPS request is the <security mode="Transport" />:
<bindings>
<webHttpBinding>
<binding name="webBindingHttps">
<security mode="Transport">
</security>
</binding>
</webHttpBinding>
</bindings>
This will effectively switch the service over to HTTPS, but if you want to have both HTTP and HTTPS to work you need to define 2 binding configurations and then have 2 identical endpoints per service, where the one uses the http bindingConfiguration and the other uses the https bindingConfiguration like so:
<bindings>
<webHttpBinding>
<binding name="webBindingHttps">
<security mode="Transport">
</security>
</binding>
<binding name="webBindingHttp">
<!-- Nothing special here -->
</binding>
</webHttpBinding>
</bindings>
<services>
<service name="TestService">
<endpoint address="" behaviorConfiguration="TestServiceAspNetAjaxBehavior"
binding="webHttpBinding" bindingConfiguration="webBindingHttps" contract="TestService" />
<endpoint address="" behaviorConfiguration="TestServiceAspNetAjaxBehavior"
binding="webHttpBinding" bindingConfiguration="webBindingHttp" contract="TestService" />
</service>
</services>

How can I host a WCF service without an SVC file in IIS

I'd like to deploy a dual interface (SOAP/REST/XML/JSON) WCF service in IIS with just a config file and the binaries and no svc file in the URL
We use VS2012 and .Net 4.5
We have something like it working, I followed a guide here:
http://blogs.msdn.com/b/rjacobs/archive/2010/04/05/using-system-web-routing-with-data-services-odata.aspx
I added a Global class with
public class Global : HttpApplication
{
void Application_Start(object sender, EventArgs e)
{
RegisterRoutes();
}
private void RegisterRoutes()
{
DataServiceHostFactory factory = new DataServiceHostFactory();
RouteTable.Routes.Add(new ServiceRoute("wrap", factory, typeof(NeOtheWrapper)));
}
}
And I used my existing web.config which defines all the endpoints:
<system.serviceModel>
<!-- Clients -->
<client>
<endpoint name="MySoftLive" address="https://backof.somewebsitett.com/Cmp.MySoft.bl/MySoft.svc" binding="basicHttpsBinding" bindingConfiguration="soapSecureBindingConfig" contract="Cmp.MySoft.BL.WCF.MySoftInterface" />
<endpoint name="MySoftTest" address="http://localhost:49957/MySoft.svc" binding="basicHttpBinding" bindingConfiguration="soapBindingConfig" contract="Cmp.MySoft.BL.WCF.MySoftInterface" />
</client>
<!-- Services -->
<services>
<service name="Cmp.MySoft.BL.NeOthe.NeOtheWrapper">
<endpoint name="rest" address="rest" behaviorConfiguration="restEndpointBehaviour" binding="webHttpBinding" bindingConfiguration="restBindingConfig" contract="Cmp.MySoft.BL.NeOthe.INeOtheWrapper"/>
<endpoint name="restSecure" address="rest" behaviorConfiguration="restEndpointBehaviour" binding="webHttpBinding" bindingConfiguration="restSecureBindingConfig" contract="Cmp.MySoft.BL.NeOthe.INeOtheWrapper"/>
<endpoint name="mex" address="mex" behaviorConfiguration="" binding="mexHttpBinding" bindingConfiguration="mexBindingConfig" contract="Cmp.MySoft.BL.NeOthe.INeOtheWrapper"/>
<endpoint name="mexSecure" address="mex" behaviorConfiguration="" binding="mexHttpsBinding" bindingConfiguration="mexSecureBindingConfig" contract="Cmp.MySoft.BL.NeOthe.INeOtheWrapper"/>
<endpoint name="soap" address="soap" behaviorConfiguration="" binding="basicHttpBinding" bindingConfiguration="soapBindingConfig" contract="Cmp.MySoft.BL.NeOthe.INeOtheWrapper"/>
<endpoint name="soapSecure" address="soap" behaviorConfiguration="" binding="basicHttpsBinding" bindingConfiguration="soapSecureBindingConfig" contract="Cmp.MySoft.BL.NeOthe.INeOtheWrapper"/>
</service>
</services>
<!-- Binding Configurations -->
<bindings>
<webHttpBinding>
<binding name="restBindingConfig">
<security mode="None"/>
</binding>
<binding name="restSecureBindingConfig">
<security mode="Transport"/>
</binding>
</webHttpBinding>
<mexHttpBinding>
<binding name="mexBindingConfig"/>
</mexHttpBinding>
<mexHttpsBinding>
<binding name="mexSecureBindingConfig"/>
</mexHttpsBinding>
<basicHttpsBinding>
<binding name="soapSecureBindingConfig">
<security mode="Transport"/>
</binding>
</basicHttpsBinding>
<basicHttpBinding>
<binding name="soapBindingConfig">
<security mode="None"/>
</binding>
</basicHttpBinding>
</bindings>
<!-- Behaviour Configurations -->
<behaviors>
<endpointBehaviors>
<behavior name="restEndpointBehaviour">
<webHttp helpEnabled="true" defaultBodyStyle="Bare" defaultOutgoingResponseFormat="Json" automaticFormatSelectionEnabled="true" faultExceptionEnabled="true" />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
<dataContractSerializer maxItemsInObjectGraph="2147483647" />
</behavior>
</serviceBehaviors>
</behaviors>
<!-- Hosting Environment Settings -->
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
</system.serviceModel>
It compiles, runs and if I browse to http://mypc:12345/wrap/rest/help I get the auto generated ASP.NET REST help page.
But, if I go to http://mypc:12345/wrap/soap/ I get 400 Bad Request.
I can't suffix that with ?wsdl to get the wsdl, or pass the url to svcutil (soap+xml not expected)
I was hoping the .SVC SOAP place holder page would appear, same as /help does for REST.
If I browse to the .svc file (it's at the same level as /wrap) that works and the soap service works, as does meta data publishing.
Am I using the wrong URL or is my configuration wrong?
If you're using WCF 4.0 or later, you can use "file-less" activation. Add something like the following to config your file:
<serviceHostingEnvironment multipleSiteBindingsEnabled="true">
<serviceActivations>
<add factory="System.ServiceModel.Activation.ServiceHostFactory"
relativeAddress="Soap.svc"
service="Cmp.MySoft.BL.NeOthe.NeOtheWrapper" />
</serviceActivations>
</serviceHostingEnvironment>
This allows you to host a WCF service without a physical .svc file. The relativeAddress is relative to the base address of the site, and you'll need to create the IIS application as usual.
See the "File-Less activation" section in A Developer's Introduction to Windows Communication Foundation 4 for more information.

WCF Configuration for https and JSON Post

As it normally happens, I accidentally overwrote my web.config file this morning that has my WCF settings already working perfectly. As you would expect, no backup...shame shame shame on me.
That being said, I can't seem to get my configuration working again.
Here is the error that I keep getting back from IIS/ASP.NET
The message with Action '' cannot be processed at the receiver, due to
a ContractFilter mismatch at the EndpointDispatcher. This may be
because of either a contract mismatch (mismatched Actions between
sender and receiver) or a binding/security mismatch between the sender
and the receiver. Check that sender and receiver have the same
contract and the same binding (including security requirements, e.g.
Message, Transport, None).
Here is an overview of what I have setup.
My Web Config:
<system.serviceModel>
<bindings>
<webHttpBinding>
<binding name="Binding1" maxReceivedMessageSize="10000000">
<readerQuotas maxArrayLength="10000000" />
<security mode="Transport"></security>
</binding>
</webHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
<services>
<service name="MyService">
<endpoint address="" binding="webHttpBinding" bindingConfiguration="Binding1" contract="IMyService" />
</service>
</services>
</system.serviceModel>
Here is the declaration in my Service Contract:
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "/SSMX", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
sqlStudio_Table_Permissions SSMX(string TableName);
Sadly I just can't seem to remember what I did in my previous configuration to make this work.
Any help would be appreciated.

How to access wcf service from another computer?

I have a wcf service and i install it as windows service.
I can access this service from 192.168.2.6 machine:
"net.tcp://192.168.2.5:2025/Services/mex".
i want to access this service from another computer using static ip and port.
How can access this service ?
I tried to connect net.tcp://staticIp:port/Services/mex and i got error :
Metadata contains a reference that cannot be resolved: 'net.tcp://[staticIP]:[port]/Services/mex'.If the service is defined in the current solution, try building the solution and adding the service reference again.
(I navigate my [port] to inside port 2025)
my config:
<system.serviceModel>
<diagnostics>
<messageLogging logMalformedMessages="true" logMessagesAtTransportLevel="true" />
</diagnostics>
<bindings>
<netTcpBinding>
<binding name="NetTcpBinding_IServices" />
</netTcpBinding>
</bindings>
<client>
<endpoint address="net.tcp://192.168.2.5:2025/Services" binding="netTcpBinding"
bindingConfiguration="NetTcpBinding_IServices" contract="myServices.IServices"
name="NetTcpBinding_IServices">
<!--<identity>
<dns value="localhost" />
</identity>-->
</endpoint>
</client>
<services>
<service name="F8ShadowWcfLib.Services">
<endpoint address="" binding="netTcpBinding" bindingConfiguration=""
contract="F8ShadowWcfLib.IServices">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexTcpBinding" bindingConfiguration=""
contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="net.tcp://192.168.2.5:2025/Services" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="false"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
Edit1:
I remove tag from config and i add it at runtime.
myService.ServicesClient myServ = new myService.ServicesClient();
EndpointAddress myEndpointAdd = new EndpointAddress(new Uri("net.tcp://[staticIP]:[port]/Services") ,
EndpointIdentity.CreateDnsIdentity("net.tcp://f8srv.f8.com.tr:2299/Services"));
myServ.Endpoint.Address = myEndpointAdd;
I got different error : The server has rejected the client credentials.
The problem is probably related to this part:
<identity>
<dns value="localhost" />
</identity>
This work work locally because localhost makes sense locally, but across a network it doesn't.
You can validate this identity in a number of ways, such as specifying the UPN (user principal name) of the user running the service, or an SPN (Server Principal Name) of the server running the service (although for this you'll have to register a SPN).
This article should explain it a little:
http://msdn.microsoft.com/en-us/library/ms733130.aspx
To allow seperate connection set AddressFilterMode : Any
and set your identy both service and client side.
This article about identy settings:
http://msdn.microsoft.com/en-us/library/ms733130.aspx
[ServiceBehavior(AddressFilterMode = AddressFilterMode.Any)]
public class Services : IServices
{
.
.
.
}

Hide REST methods from SOAP WCF Client

I created a simple WCF Service interface:
namespace ApiDoc.SampleApi
{
/// <summary>
/// Contract
/// </summary>
/// <webMethodsPrefix>Web</webMethodsPrefix>
[ServiceContract(Namespace = "apidoc.sampleapi.com", Name = "SampleApi")]
public interface IService
{
[WebGet( UriTemplate = "Add?value1={value1}&value2={value2}&apiKey={apiKey}", BodyStyle = WebMessageBodyStyle.Bare)]
AddRs AddWithHttpGet(int value1, int value2, string apiKey);
[WebInvoke(Method = "POST", UriTemplate = "Add", BodyStyle = WebMessageBodyStyle.Bare)]
AddRs Add(AddRq rq);
}
}
In this case it is just a simple Add operation.
It works well for Xml, Soap and Json. Both Get and Post.
The issue I am having is in Soap when I create a Service Reference to this service. I can call both functions "Add" and "AddWithHttpGet", while I only would like to see "Add".
I originally thought it was related to using "OperationContract" attribute, but it seems like it is not used any longer. I tried adding this attribute only to POST Add, but it doesn't make any difference. I am using ASP.NET 4.0.
Another solution would be to create a different IService for Soap, but I would rather keep this all in one interface.
Here is my config file:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<bindings>
<webHttpBinding>
<binding name="jsonHttpBinding" />
<binding name="xmlHttpBinding" />
</webHttpBinding>
</bindings>
<services>
<service name="ApiDoc.SampleApi.Service" behaviorConfiguration="ApiDocSampleApiBehavior">
<host>
<baseAddresses>
<add baseAddress="http://localhost/ApiDoc.SampleApi/" />
</baseAddresses>
</host>
<endpoint name="soap" address="" binding="basicHttpBinding" bindingNamespace="apidoc.sampleapi.com" contract="ApiDoc.SampleApi.IService" lockAttributes="bindingNamespace" />
<endpoint name="json" address="json" binding="webHttpBinding" bindingNamespace="apidoc.sampleapi.com" bindingConfiguration="jsonHttpBinding" contract="ApiDoc.SampleApi.IService" behaviorConfiguration="JsonBehavior" />
<endpoint name="xml" address="xml" binding="webHttpBinding" bindingNamespace="apidoc.sampleapi.com" bindingConfiguration="xmlHttpBinding" contract="ApiDoc.SampleApi.IService" behaviorConfiguration="XmlBehavior" />
<endpoint name="mex" address="mex" binding="mexHttpBinding" bindingNamespace="apidoc.sampleapi.com" contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="JsonBehavior">
<webHttp defaultOutgoingResponseFormat="Json" />
</behavior>
<behavior name="XmlBehavior">
<webHttp defaultOutgoingResponseFormat="Xml" />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="ApiDocSampleApiBehavior">
<serviceMetadata httpGetEnabled="true" httpGetUrl="" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true" minFreeMemoryPercentageToActivateService="0" />
</system.serviceModel>
...
I would create a separate interface.
Think of your interface like a real contract between two parties. You are using it to define what operations are available to a client. If it's in the contract, it's available.
Instead of looking for some sort "invisible ink" that would make certain parts of the contract available to certain clients, I'd simply create two contracts.
I'm a little confussed. The OperationContractAttribute is required. A WebGetAttribute on it's own shouldn't do anything, as the method is not expossed as an operation.
As for hiding an operation, that is not possable either. If the interface is your contract, and you definatly want two different contracts then you will need two different interfaces.
If you don't want to duplicate your code then you can still use inheritace. Have one interace define your SOAP operation Add and then inherit from that to add AddWithHttpGet. Then by targeting the different interfaces in your endpoints, the SOAP endpoint would have one operation and REST endpoint would have two.
try this you want hide webmethod form soap header in c#
[SoapDocumentMethod(ParameterStyle = SoapParameterStyle.Bare), WebMethod]
public string Operation(RIL_OB_MSG RIL_OB_MSG)
{
}

Resources