逐步解說:建立和存取 WCF 服務
WCF服務程式庫與WCF服務應用程式的不同
WCF服務應用程式原始碼
ISendXmlWcfService.svc
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Xml.Linq;
namespace SendXmlWcfService
{
public class SendXmlWcfService : ISendXmlWcfService
{
public string GetData(string value)
{
XDocument xml = XDocument.Parse(value);
return string.Format("To {0}\r\n {1}\r\n {2} {3}", xml.Root.Element("to").Value, xml.Root.Element("body").Value,xml.Root.Element("heading").Value,xml.Root.Element("from").Value);
}
}
}
ISendXmlWcfService.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
namespace SendXmlWcfService
{
[ServiceContract]
public interface ISendXmlWcfService
{
[OperationContract]
string GetData(string value);
}
}
測試WCF服務應用程式-主控台應用程式
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
StringBuilder builder = new StringBuilder("<note>" +
"<to>Tove</to>" +
"<from>Jani</from>" +
"<heading>Reminder</heading>" +
"<body>Don't forget me this weekend!</body>" +
"</note>");
ServiceReference1.SendXmlWcfServiceClient client = new ServiceReference1.SendXmlWcfServiceClient();
Console.WriteLine(client.GetData(builder.ToString()));
Console.ReadLine();
}
}
}
沒有留言:
張貼留言