Friday, 15 June 2012

c# - System.InvalidOperationException when calling WCF Service -



c# - System.InvalidOperationException when calling WCF Service -

i'm trying create wcf callback service nettcpbinding. when seek phone call method of service next exception:

an unhandled exception of type 'system.invalidoperationexception' occurred in system.servicemodel.dll additional information: instancecontext provided channelfactory contains userobject not implement callbackcontracttype 'client.wcfservice.ihostfunctionscallback'.

i've added service reference instead of using svcutil.exe i've searched net fixing problem, haven't found solution yet.

here's implementation:

ihostfunctions.cs (part of hostlibrary)

using system.servicemodel; namespace hostlibrary { [servicecontract(callbackcontract = typeof(icallback))] public interface ihostfunctions { [operationcontract] void opensession(); } }

icallback.cs (part of hostlibrary)

using system.servicemodel; namespace hostlibrary { public interface icallback { [operationcontract] void oncallback(); } }

hostfunctions.cs (part of hostlibrary)

using system; using system.servicemodel; using system.timers; namespace hostlibrary { [servicebehavior(concurrencymode = concurrencymode.multiple)] public class hostfunctions : ihostfunctions { #region implementation of ihostfunctions public static icallback callback; public static timer timer; public void opensession() { console.writeline("> session opened @ {0}", datetime.now); callback = operationcontext.current.getcallbackchannel<icallback>(); timer = new timer(1000); timer.elapsed += ontimerelapsed; timer.enabled = true; } void ontimerelapsed(object sender, elapsedeventargs e) { callback.oncallback(); } #endregion } }

callback.cs (part of client)

using system; using hostlibrary; namespace client { public class callback : icallback { #region implementation of icallback public void oncallback() { console.writeline("> received callback @ {0}", datetime.now); } #endregion } }

program.cs of service

using system; using system.servicemodel; using hostlibrary; namespace wcf_tcp_callbacks { internal static class programme { private static void main(string[] args) { using (var sh = new servicehost(typeof (hostfunctions))) { sh.open(); console.writeline("service started."); console.readline(); console.writeline("stopping service..."); sh.close(); } } } }

program.cs of client

using system; using system.globalization; using system.servicemodel; using system.threading; using client.wcfservice; namespace client { internal class programme { private static void main(string[] args) { var callback = new callback(); using (var proxy = new hostfunctionsclient(new instancecontext(callback))) { proxy.opensession(); } console.readline(); } } }

the code http://adamprescott.net/2012/08/15/a-simple-wcf-service-callback-example/ nettcpbinding.

by default wcf effort dispatch using available synchronizationcontext. problem callback ui thread blocked in outbound call. phone call dispatch need tell wcf not utilize synchronizationcontext – 1 time again using callbackbehavior attribute:

[callbackbehavior(concurrencymode=concurrencymode.reentrant, usesynchronizationcontext=false)] public class callback : icallback { .... }

for farther detail link http://www.dotnetconsult.co.uk/weblog2/permalink,guid,b891610a-6b78-4b54-b9a6-4ec81c82b7c0.aspx , 1 more post describe farther http://stefanoricciardi.com/2009/08/28/file-transfer-with-wcp/

c# wcf callback

No comments:

Post a Comment