Guest User

Untitled

a guest
Aug 14th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. How should I consume a WCF service using MVC3
  2. // example of ws
  3. public class Service1 : IService1
  4. {
  5. public string GetData( int value )
  6. {
  7. return string.Format( "You entered: {0}", value );
  8. }
  9.  
  10. public CompositeType GetDataUsingDataContract( CompositeType composite )
  11. {
  12. if( composite == null )
  13. {
  14. throw new ArgumentNullException( "composite" );
  15. }
  16. if( composite.BoolValue )
  17. {
  18. composite.StringValue += "Suffix";
  19. }
  20. return composite;
  21. }
  22. }
  23.  
  24. // example of mvc action
  25. public class HomeController : Controller
  26. {
  27. public ActionResult Index()
  28. {
  29. IService1 service = new Service1();
  30.  
  31. service.GetDataUsingDataContract(....)
  32.  
  33. ViewBag.Message = "Welcome to ASP.NET MVC!";
  34.  
  35. return View();
  36. }
  37. }
  38.  
  39. public ActionResult Index()
  40. {
  41. SettingsModel config = null;
  42.  
  43. // Set up a channel factory to use the webHTTPBinding
  44. using (WebChannelFactory<IChangeService> serviceChannel =
  45. new WebChannelFactory<IChangeService>(new Uri(baseServiceUrl )))
  46. {
  47. IChangeService channel = serviceChannel.CreateChannel();
  48. config = channel.GetSysConfig();
  49. }
  50.  
  51. ViewBag.Message = "Service Configuration";
  52.  
  53. return View(config);
  54. }
Add Comment
Please, Sign In to add comment