c# - Adding WCF service to existing application? -
i have existing application has requirement interacted mobile device. mobile device has wifi connection, , connecting pc hosting main application on lan. mobile device needs add/edit/find/delete objects main application maintaining. main application encapsulates functionality in simple repository classes.
i believe approach add wcf service main application exposes set of methods mobile device can call against. have looked wcf today , tried setup example application, when called wcf methods unable access data, such feel wcf service running in own application domain , such has no access same static classes in main application.
if setup wcf service project in vs 2008/2010, how can run under same application domain main winforms application, remote application on lan can communicate data application.
below sample winform
using system; using system.servicemodel; using system.windows.forms; using dataproject; namespace windowsformsapplication1 { public partial class form1 : form { public testdataproject.datastore datastore = testdataproject.datastore.getinstance(); public form1() { initializecomponent(); datastore.add(new myobj { id = 1, data = "hello" }); datastore.add(new myobj { id = 2, data = "world" }); datastore.add(new myobj { id = 3, data = "item3" }); datastore.add(new myobj { id = 4, data = "item4" }); datastore.add(new myobj { id = 5, data = "fiver" }); } } }
what need wcf service, access testdataproject.datastore.getinstance();
edit
i achieved by
using system; using system.servicemodel; using system.servicemodel.description; using system.windows.forms; using dataproject; using testdataproject; namespace windowsformsapplication1 { public partial class form1 : form { public testdataproject.datastore datastore = testdataproject.datastore.getinstance(); public form1() { initializecomponent(); datastore.add(new myobj { id = 1, data = "hello" }); datastore.add(new myobj { id = 2, data = "world" }); datastore.add(new myobj { id = 3, data = "item3" }); datastore.add(new myobj { id = 4, data = "item4" }); datastore.add(new myobj { id = 5, data = "fiver" }); servicehost host = new servicehost(typeof(simpleservice), new uri("http://localhost:8001/metadatasample")); try { // check see if service host has servicemetadatabehavior servicemetadatabehavior smb = host.description.behaviors.find<servicemetadatabehavior>(); // if not, add 1 if (smb == null) smb = new servicemetadatabehavior(); smb.httpgetenabled = true; smb.metadataexporter.policyversion = policyversion.policy15; host.description.behaviors.add(smb); // add mex endpoint host.addserviceendpoint( servicemetadatabehavior.mexcontractname, metadataexchangebindings.createmexhttpbinding(), "mex" ); // add application endpoint host.addserviceendpoint(typeof(isimpleservice), new wshttpbinding(), ""); // open service host accept incoming calls host.open(); // service can accessed. console.writeline("the service ready."); console.writeline("press <enter> terminate service."); console.writeline(); console.readline(); // close servicehostbase shutdown service. //host.close(); } catch (communicationexception commproblem) { console.writeline("there communication problem. " + commproblem.message); console.read(); } } public void display(string msg) { messagebox.show(msg); } } [servicecontract] public interface isimpleservice { [operationcontract] string test(); [operationcontract] string getobjdesc(int id); [operationcontract] myobj getobject(int id); } public class simpleservice : isimpleservice { #region implementation of isimpleservice public string test() { return "hello world"; } public string getobjdesc(int value) { myobj obj = datastore.getinstance().get(value); if (obj != null) { return obj.data; } return ""; } public myobj getobject(int id) { return datastore.getinstance().get(id); } #endregion } }
with app.config containing
<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.servicemodel> <services> <service name="windowsformsapplication1.simpleservice"> </service> </services> <behaviors> <servicebehaviors> <behavior name="simpleservicebehavior"> <servicemetadata httpgetenabled="true" policyversion="policy15" /> </behavior> </servicebehaviors> </behaviors> </system.servicemodel> </configuration>
i use wcf test client on url http://localhost:8001/metadatasample
the main issue suffered service starting automatically, can disabled in vs2010 project setting. , other issue uac, given visual studio not set administrator debugger failed host service, fixed adding windowsformapplication1.manifest file containing
<?xml version="1.0" encoding="utf-8" standalone="yes"?> <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestversion="1.0">” <trustinfo xmlns="urn:schemas-microsoft-com:asm.v3">” <security> <requestedprivileges> <requestedexecutionlevel level="requireadministrator"/> </requestedprivileges> </security> </trustinfo> </assembly>
you have created wcf web service project, running inside web service process (normally iis), not inside windows forms process , won't have access data in static classes , properties in windows forms process.
it sounds simplest option host wcf service inside windows forms application instead. don't want go detail on how there number of resources available on web (also can hardly claim expert!), might want try following article starting point:
Comments
Post a Comment