Silverlight client got NotFound error from WCF - wcf

There are a lot of article on this subject, but neither helped me. I am trying to implement service which could be used without "Add Service Ref..." mostly with advice from hhttp://www.netfxharmonics.com/2008/11/Understanding-WCF-Services-in-Silverlight-2 .
I made small project to reproduce problem.
http://hotfile.com/dl/96710945/9991ac3/SilverlightApplication8.zip.html
I tried solution like :
- Handling Faults in Silverlight
- Cross domain policy
etc
All standard checks are done like, service is active and reachable, client succeed to create channel etc.
I am stack whole week with this problem and I can not figure it out.
Every help is appreciate.

Denis,
try to create service in your web host project. Add service there and then you'll have a choice to add it as reference in your silverlight application. Just add a service in SilverlightApplication8.Web. Right click on SilverlightApplication8.Web --> Add new Item --> On left choose Silverlight --> Silverlight-enabled WCF service. And then add reference to your SilverlightApplication8.

I did not go in deeper investigation, but I assume that service implementation class type was not good.
So my factory class looked like:
Public Class TimeServiceFactory
Inherits System.ServiceModel.Activation.ServiceHostFactoryBase
Public Overrides Function CreateServiceHost(ByVal constructorString As String, ByVal baseAddresses() As System.Uri) As System.ServiceModel.ServiceHostBase
Dim host As New ServiceHost(constructorString, baseAddresses)
It needs to be changed in:
Public Class TimeServiceFactory
Inherits System.ServiceModel.Activation.ServiceHostFactoryBase
Public Overrides Function CreateServiceHost(ByVal constructorString As String, ByVal baseAddresses() As System.Uri) As System.ServiceModel.ServiceHostBase
Dim host As New ServiceHost(GetType(TimeService), baseAddresses)
Difference is I did not pass constructorString (which has information of type of service implementation class) , I passed GetType(TimeService) instate, which provided correct type information.

Related

wcf PerSession (Wshttpbinding) with Portable Class Library (PCL)

seems that Portable Class Library does not support PerSession (Wshttpbinding) required for this.
Is there any work around for this?
i have xamarin forms application (server client) that users can connect to their own database and get or update data.
once the user provide his username and password some information must be stored to their sessions like database connection string and when data request from user then the appropriate connection string that is stored to his session will be used for that database and send the data back.
otherwise without persession i have to pass on every function the connection string from the client.
how can i avoid this? Portable Class Library does not support wshttpbinding and therefore i cannot use PerSession
any help?
Too much Headache
The obstacles are too much for anyone working with the following combination
Xamarin Forms
PCL (Portable Class Libray)
WCF
IClientMessageInspector
The Problem:
I was need to have a multi client application that each one can connect to his own database ( same schema all )
For this purpose i was looking to store some information at a session level like (Connection String,User Information) so when a method called to know from which database i will retreive the data .
Problem # 1 ( took me 5 days to understand and realized)
that Sessions cannot be worked with PCL ( WCF PerSession) why? Because PerSession Needs WsHttpBinding but PCL Does not support WsHttpBinding only BasicHttp
Problem # 2 (took me 3 days to understand why is not working)
Then i saw many posts here and there about implementing IClientMessageInspector (see examples online how to implement IClientMessageInspector it's easy) so every time a client request a call to send some extra info to the server together with the call in my case was the connectionString. Here i was getting an exception that
NotImplementedException. Why? because of this BUG in MONO
https://bugzilla.xamarin.com/show_bug.cgi?id=40064
in simple words
When creating a WCF client in a PCL (targetting .NET Core) you must use EndpointBehaviors and not Behaviors. This works fine in a Windows RT application, but Mono has not implemented this so produces NotImplementedException in Android and iOS.
The answer is on the link above by another reply from someone else and i thank him for that .
In simple words
Instead of
MyService.Endpoint.EndpointBehaviors(New MyBehavior)
Use
Dim prop = MyService.Endpoint.GetType.GetTypeInfo.GetDeclaredProperty("Behaviors")
Dim obj = CType(prop.GetValue(MyService.Endpoint), KeyedCollection(Of Type, IEndpointBehavior))
obj.Add(New Behavior)
and instead of
clientRuntime.ClientMessageInspectors.Add(New MyInspector)
Use
Public Sub ApplyClientBehavior(endpoint As ServiceEndpoint, clientRuntime As ClientRuntime) Implements IEndpointBehavior.ApplyClientBehavior
Dim prop = clientRuntime.GetType.GetTypeInfo.GetDeclaredProperty("MessageInspectors")
Dim obj = CType(prop.GetValue(clientRuntime), ICollection(Of IClientMessageInspector))
obj.Add(New MyInspector)
End Sub
No changes needed to the configuration file
Hope this help someday someone.

Accessing functions of added web service

I have created my own WebService in VS 2010. My project is called sampleWebService and inside my project I have successfully added/connected to another WebService this is called practiceService.
sampleWebService has just the basic Hello World auto generated code in it, however, practiceService has Web Method Functions that handle database calls such as getFirstName, getLastName,...
My question isn't how to extract the data really since I know you have to use either JSON or SOAP. I'm just wondering what I have to type into my code to be able to see the functions and methods that are in my added web reference practiceService so I can connect to them.
Or maybe this is accomplished by using JSON or SOAP
Right now my code for my sampleWebService page is just as is:
<ToolboxItem(False)> _
Public Class Service1
Inherits System.Web.Services.WebService
<WebMethod()> _
Public Function HelloWorld() As String
Return "Hello World"
End Function
End Class
You can have a look at the proxy class that was generated when you added the web reference. Also when you create the object of practice service it will give you access to all the web methods exposed in that web service.
To access the web method you have to do something like
ServerName.WebServiceName CallWebService =
new ServerName.WebServiceName();
String sGetValue = CallWebService.MethodName();
Label1.Text = sGetValue;

How to create a WCF web service within an ASP.NET application that can return instances of an interface as a transparent proxy

My use-case:
I already have a working ASP.NET application
I would like to implement a new Web Service as part of that application
I am supposed to use a WCF service (*.svc), not an ASP.NET web service (*.asmx)
The service needs to have one operation, let’s call it GetInterface(), which returns instance of an interface. This instance must reside on the server, not be serialized to the client; methods called on that interface must execute on the server.
Here’s what I tried (please tell me where I went wrong):
For the purpose of testing this, I created a new ASP.NET Web Application project called ServiceSide.
Within that project, I added a WCF Service using “Add → New Item”. I called it MainService. This created both a MainService class as well as an IMainService interface.
Now I created a new Class library project called ServiceWorkLibrary to contain only the interface declaration that is to be shared between the client and server, nothing else:
[ServiceContract]
public interface IWorkInterface
{
[OperationContract]
int GetInt();
}
Back in ServiceSide, I replaced the default DoWork() method in the IMainService interface as well as its implementation in the MainService class, and I also added a simple implementation for the shared IWorkInterface. They now look like this:
[ServiceContract]
public interface IMainService
{
[OperationContract]
IWorkInterface GetInterface();
}
public class MainService : IMainService
{
public IWorkInterface GetInterface()
{
return new WorkInterfaceImpl();
}
}
public class WorkInterfaceImpl : MarshalByRefObject, IWorkInterface
{
public int GetInt() { return 47; }
}
Now running this application “works” in the sense that it gives me the default web-service page in the browser which says:
You have created a service.
To test this service, you will need to create a client and use it to
call the service. You can do this using the svcutil.exe tool from the
command line with the following syntax:
svcutil.exe http://localhost:59958/MainService.svc?wsdl
This will generate a configuration file and a code file that contains
the client class. Add the two files to your client application and use
the generated client class to call the Service. For example:
So on to the client then. In a separate Visual Studio, I created a new Console Application project called ClientSide with a new solution. I added the ServiceWorkLibrary project and added the reference to it from ClientSide.
Then I ran the above svcutil.exe call. This generated a MainService.cs and an output.config, which I added to the ClientSide project.
Finally, I added the following code to the Main method:
using (var client = new MainServiceClient())
{
var workInterface = client.GetInterface();
Console.WriteLine(workInterface.GetType().FullName);
}
This already fails with a cryptic exception in the constructor call. I managed to fix this by renaming output.config to App.config.
I notice that the return type of GetInterface() is object instead of IWorkInterface. Anyone know why? But let’s move on...
Now when I run this, I get a CommunicationException when calling GetInterface():
The underlying connection was closed: The connection was closed unexpectedly.
How do I fix this so that I get the IWorkInterface transparent proxy that I expect?
Things I’ve tried
I tried adding [KnownType(typeof(WorkInterfaceImpl))] to the declaration of WorkInterfaceImpl. If I do this, I get a different exception in the same place. It is now a NetDispatcherFaultException with the message:
The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter http://tempuri.org/:GetInterfaceResult. The InnerException message was 'Error in line 1 position 491. Element 'http://tempuri.org/:GetInterfaceResult' contains data from a type that maps to the name 'http://schemas.datacontract.org/2004/07/ServiceSide:WorkInterfaceImpl'. The deserializer has no knowledge of any type that maps to this name. Consider using a DataContractResolver or add the type corresponding to 'WorkInterfaceImpl' to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding it to the list of known types passed to DataContractSerializer.'. Please see InnerException for more details.
The InnerException mentioned is a SerializationException with the message:
Error in line 1 position 491. Element 'http://tempuri.org/:GetInterfaceResult' contains data from a type that maps to the name 'http://schemas.datacontract.org/2004/07/ServiceSide:WorkInterfaceImpl'. The deserializer has no knowledge of any type that maps to this name. Consider using a DataContractResolver or add the type corresponding to 'WorkInterfaceImpl' to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding it to the list of known types passed to DataContractSerializer.
Notice how this seems to indicate that the system is trying to serialize the type. It is not supposed to do that. It is supposed to generate a transparent proxy instead. How do I tell it to stop trying to serialize it?
I tried adding an attribute, [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)], to the WorkInterfaceImpl class. No effect.
I tried changing the attribute [ServiceContract] on the IWorkInterface interface (declared in the shared library ServiceWorkLibrary) to [ServiceContract(SessionMode = SessionMode.Required)]. Also no effect.
I also tried adding the following magic system.diagnostics element to the Web.config in ServerSide:
<system.diagnostics>
<!-- This logging is great when WCF does not work. -->
<sources>
<source name="System.ServiceModel" switchValue="Information, ActivityTracing" propagateActivity="true">
<listeners>
<add name="traceListener" type="System.Diagnostics.XmlWriterTraceListener" initializeData="c:\traces.svclog" />
</listeners>
</source>
</sources>
</system.diagnostics>
This does generate the c:\traces.svclog file as promised, but I’m not sure I can make any sense of its contents. I’ve posted the generated file to pastebin here. You can view this information in a more friendly UI by using svctraceviewer.exe. I did that, but frankly, all that stuff doesn’t tell me anything...
What am I doing wrong?
The use-case I am describing is not directly supported by WCF.
The accepted work-around is to return an instance of EndpointAddress10 which points to the service for the “other” interface. The client must then manually create a Channel to access the remote object. WCF doesn’t properly encapsulate this process.
An example that demonstrates this is linked to from the MSDN article “From .NET Remoting to the Windows Communication Foundation (WCF)” (find the text that says “Click here to download the code sample for this article”). This example code demonstrates both .NET Remoting as well as WCF. It defines an interface that looks like this:
[ServiceContract]
public interface IRemoteFactory
{
IMySessionBoundObject GetInstance();
[OperationContract]
EndpointAddress10 GetInstanceAddress();
}
Notice that the interface-returning method is not part of the contract, only the one that returns an EndpointAddress10 is marked with [OperationContract]. The example calls the first method via Remoting, where it correctly creates a remote proxy as one would expect — but when using WCF it resorts to the second method and then instantiates a separate ChannelFactory with the new endpoint address to access the new object.
What is MainServiceClient()? It is the class marshaling the client messages to the server.
You should take a look at a related SO post on returning interfaces as parameters in WCF. ServiceKnownTypeAttribute may be helpful.
Sessions may also be what you're looking for MarshalByRef as it relates to .NET Remoting behaviors.
Another approach (as mentioned on MSDN Forums) is to return the EndpointAddress of the service interface instead of the interface itself.
WCF does serialize everything - regardless of the binding. The best approach you should take if you need to communicate with the service on the same system is to use IPC transport binding (net.pipe).
What you are trying to do is a direct violation of the SOA Tenet: "Services share schema and contract, not class". What this means it that you don't actually pass implementation code from the service to its consumers, just the return values that are specified in the contract itself.
The main focus of WCF and SOA in general is interoperability, meaning services should be accessible to clients of any platform. How would a Java or C++ consumer be able to use this service you are designing? Short answer is that it couldn't, which is why you will find it difficult if not impossible to serialize this code over messaging standards like SOAP.
A more appropriate way to structure this code would be to host each implementation of IWorkerInterface as its own service (it has been defined as a service contract, after all), and expose each service on a different endpoint. Instead of MainService behaving as remote factory for proxies to an IWorkerInterface, it could act a as an endpoint factory to the different services you have set up. Endpoint metadata could easily be serialized and provided to the client by IMainService. The client could then take that metadata and construct a proxy to the remote implementation, either through some custom IServiceProxy implementation, or even through the objects already provided to you by WCF (such as the ChannelFactory).

Ninject into WCF REST Service

I'm using the WCF REST Template where services are implemented with just a class and registered in the Global.ascx (much like MVC controllers are).
RouteTable.Routes.Add(new ServiceRoute("Games/Games", new WebServiceHostFactory(), typeof(Games.Games)));
Games.Games has a ctor accepting a Dal.Games.IGames and I have a NinjectModule with the Bindings ready but I cant for the life of me figure out where to pass the kernel to to have it control the creation of the service classes.
My services dont have a markup (svc) file so I'm guessing that it will have something do with replacing the WebServiceHostFactory with one from Ninject. I was able to find one in the Ninject Web extension but just dropping that in didnt change anything not to mention I coulnt find anywhere to setup the kenel in that class.
Any solutions, hints or tips are greatly appreciated.
Let me preface this by saying, someone who actually knows inner workings of Ninject could probably provide a much cleaner solution. I've been wrestling with the same issue as you mentioned though.
Mostly through trial & error I determined that if you make the following code changes in the Ninject.Extensions.Wcf library, Ninject will work its magic on your service classes.
In NinjectServiceHostFactory.cs, i changed the base class and the type passed to .Get<T>
public class NinjectServiceHostFactory : WebServiceHostFactory //<-- Changed base class
{
protected override ServiceHost CreateServiceHost( Type serviceType, Uri[] baseAddresses )
{
var serviceTypeParameter = new ConstructorArgument( "serviceType", serviceType );
var baseAddressesParameter = new ConstructorArgument( "baseAddresses", baseAddresses );
return KernelContainer.Kernel.Get<NinjectServiceHost>( serviceTypeParameter, baseAddressesParameter );
}
}
In the NinjectServiceHost.cs i changed the base class to WebServiceHost.
Also, I added this reference to both:
using System.ServiceModel.Web;
I'm sure this solution breaks this extension for other WCF service types so hopefully a Ninject guru will come along and provide a real solution.

When does ServiceHost get created and expire

I have code as following:
Public Class xxxSvcHostFactory
Inherits ServiceHostFactory
Protected Overrides Function CreateServiceHost(ByVal serviceType As Type, ByVal baseAddresses As Uri()) As ServiceHost
Dim result As New WebServiceHost2(serviceType, True, baseAddresses)
Return result
End Function
End Class
Service contract is defined as below:
<ServiceContract()>
Public Interface IxxxSvc
<Description("")>
<OperationContract()>
<WebGet(ResponseFormat:=WebMessageFormat.Json,
UriTemplate:="CustomerDetails?id={CustomerId}")>
Function GetCustomerDetails(ByVal CustomerId As String) As Customer
End Interface
Public Class MySvc
Implements IxxxSvc
Public Function GetCustomerDetails(ByVal CustomerId As String) As Customer Implements IxxxSvc.GetCustomerDetails
.
.
.
End Function
End Class
When would CreateServiceHost gets executed?
Is it for every call, or for every transport session, or when the application startsup?
When does the ServiceHost expire?
If I implement static variable it is available through multiple sessions (say from IE and Firefox). How can I maintain static variable for a particular session (say if I access from IE, the same session should not be shared when I access from FF).
I am using WCF REST functionality in my application (core REST and not REST Starter kit).
thanks
It depends! :-) As always.....
If you host this service in IIS by using a MyService.svc file, then IIS will instantiate the WebServiceHost for each incoming request and spin up a service class instance to handle the request (ok, it's probably doing some caching on this - however, not quite clear how and how long the host will live etc.). IIS is said to have "message-based activation" - e.g. potentially each incoming message/request will activate the WebServiceHost.
When you self-host in a Windows NT Service, console app etc., it's obviously totally up to you - you create the WebServiceHost at your discretion, and then it's up and running until you explicitly tear it down (or an unhandled exception brings it down). Self-hosting gives you a bit more control over lifetime of your WebServiceHost.
Check out Hosting And Consuming WCF Services on MSDN - has lots of interesting info on hosting and lifetime of your service host and so forth.