I used Clear Image Com dll to make a Image processing service libray.
I have a interface Imagery for service contract which contains a methodLoadBitmapFromFile(imgFileName). My service class overrides this method and use clear image com dll and its method to cropping an image.
further I created a Windows form for consume this service but when
Imagery img;
img = new ServiceReference1.ImageryClient();
img.LoadBitmapFromFile(imgFileName);
pictureBox1.Image = img.GetBitmap();
Imagery is my service contract.I create a the service which refers as ServiceReference1.
LoadBitmapFromFile(imgFileName)--this method is actually defined in service class.
when it call from client I get following error:
{"The underlying connection was closed: The connection was closed unexpectedly."}
what is wrong in my case?
Is it the problem of using Com dll in service layer.?
Can anyone help me?
You might want to add try catch block to capture exception and than create a new connection. Possible cause could be timeout.
Related
Getting resource error when accessing a cloud data service through a vb.net class from my Web App.
I have written a vb.net class library (call it MYAPPIF) referencing a wcf data service with common functions that work on these. For example I may want to save a transaction, I will do it through this class library. This class library is then referenced by my web app and with "Imports MYAPPIF" I can see the common functions and I can see the cloud service reference.
dim oMyAppIF = new MyAppIF.class
dim oCloudContext = oMyAppIF.GetWCFConnection() ' a function in class
oQueryList = (From seldata In oCloudContext.vwContractList
Order By seldata.cnname
Select seldata).ToList()
The web site compiles fine but when I run it I get the following error :
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Data.Services.Client.DataServiceClientException: Resource not found for the segment 'vwContractList'.
Stack Trace:
[DataServiceClientException: Resource not found for the segment 'vwContractList'.]
System.Data.Services.Client.QueryResult.ExecuteQuery() +487
System.Data.Services.Client.DataServiceRequest.Execute(DataServiceContext context, QueryComponents queryComponents) +186
Do not invoke the service by adding a service reference in the class library project, because the class library will use the WebConfig file of the current Web APP project instead of the configuration of the class library. You can copy the configuration under servicemodel into the current project, or use the channel factory call to write the configuration dynamically.
public string Invocation()
{
//ServiceReference1.Service1Client client = new ServiceReference1.Service1Client();
//return client.GetData(34);
ChannelFactory<IService1> factory = new ChannelFactory<IService1>(new BasicHttpBinding(), new EndpointAddress("http://10.157.18.36:12000/service1.svc"));
IService1 sv = factory.CreateChannel();
return sv.GetData(46);
}
https://learn.microsoft.com/en-us/dotnet/framework/wcf/feature-details/how-to-use-the-channelfactory
Feel free to let me know if there is anything I can help with.
In my server side console program, multiple ServiceHost objects are created and working in PerSession mode, not singleton.
Therefore, a new server side object will be created when a new session started.
The problem is: How can I know which is the right ServiceHost object linked with the new created server side object? I do need this link information for further operations.
Can I get the ServiceHost information from the new created server side object? By converting it into another interface or class?
Or, can I hook the instance creating process of ServiceHost?
Any idea?
Thanks a lot.
Try the OperationContext - it has a .Host property:
var host = OperationContext.Current.Host;
See the MSDN documentation for more details.
I have a WCF service with several operation.
Each operation has the 'PrincipalPermission' tag, something like this :
[PrincipalPermission(SecurityAction.Demand, Role = "Administrator")]
public ProductsDto GetAllProducts()
{
// Do operation here ...
}
Problem is - if the user is not part of the role 'Administratir' - the service throws the exception
Request for principal permission failed
This corrupts the client's channel to a 'Faulted' state.
I want to be able to catch this somehow and send the client a 'Fault' message,
so that the client knows he tried to do something he shouldn't, without faulting the channel !
I tried using a 'try-catch' block inside the operation, but it didn't help.
The exception occurs 'outside' the operation itself.
How can I solve this ?
There's a special interface IErrorHandler that you can implement and hook up with the WCF service to handle the exception. Using IErrorHandler will let you handle both the security exceptions and any exceptions thrown by the serialization code.
Another note on the client's channel getting into a faulted state: I'd recomment creating a new client object for each unit of work you perform from the client. Reusing the client object opens up for this kinds of problems.
I am new to WCF. Is the client connected when the service is instantiated
SampleService client = new SampleService();
Or does it connect and disconnect when a method is called on the client?
client.sampleMethod();
I want to connect to the client and send multiple messages across the same connection. Is this possible?
To answer your second question: the connection is kept open by default.
For the first, and as an clarification to above statement: it depends on the binding. For starters, there is no Connect() method in ClientBase<TChannel> (which a WCF proxy client inherits from), so there'll be no use calling it since that'll throw a compiler error.
A connection to the service is made upon the first call to the service; the constructor does nothing more than bring the client object in a usable state, it does not connect to the service.
It is connected when you use the service. Also you would need to call client.Close() if you would write it like that. We mostly write our service calls like this:
using(var client = new SampleServiceClient()){
client.MethodA();
client.MethodB();
}
So yes you can call multiple methods on one instance of the service. By writing the using statement you make sure that the service is disposed after usage.
Or if you like:
var client = new SampleServiceClient();
client.MethodA();
client.MethodB();
client.Close();
You will need to call the generated proxy it's Connect method.
e.g.
SampleServiceProxy client = new SampleServiceProxy();
client.Connect()
client.SampleMethod()
I'm putting together a WCF service using net.tcp and netTcpBinding to get duplex comms with my Silverlight client. I call into the service from the Silverlight app and the service calls out to another server, passing it a callback method in the WCF service class. The remote server calls back several times and each time it does, the WCF service uses the callbackchannel to send the data to the Silverlight client. It all works nicely most of the time.
If the user puts in a big request, I get a TimeoutException after a large number of callbacks have already worked. (Clearly, there's some work to do elsewhere to prevent this but I'd like to robustify the service, first.)
I was expecting to do some kind of 'if (client.ConnectionState == faulted)' check before trying to call back to the Silverlight client but I can't seem to find the object that holds the state of the connection. Is there one? Am I approaching this from the wrong side?
This is my first venture into a service net.tcp and duplex. I just moved house and my WCF bible is still in a box. Somewhere. :-) So, I can't do my usual background reading.
Any pointers would be gratefully received. Here's some bare code in case my description is too soupy:
private IActiveDirectoryClient client;
private AsyncSearchRunner runner;
public void Search(Request request)
{
this.client = OperationContext.Current.GetCallbackChannel<IActiveDirectoryClient>();
runner = new AsyncSearchRunner();
runner.Run(request.SearchRoot, request.SearchFilter, request.PageSize,
System.DirectoryServices.Protocols.SearchScope.Subtree, SendObjects);
}
private void SendObjects(List<DirectoryObject> items)
{
Response response = new Response();
response.DirectoryObjects = items.ToArray();
client.SendResponse(response);
}
Yes, there is a State property that is defined in the ClientBase<> class (all the proxy classes are derived from ClientBase<>).
There are some proxy wrappers out there that handle fault states of the connection and re-establish connections as needed. Google for "wcf proxy wrapper".
You can also home-brew something if you use some kind of ServiceLocator pattern.