WCF创建到使用到发布
1,在VS里面新建一個類庫項目
2,向類庫項目里添加WCF服務(wù)文件
3.按照WCF約束規(guī)范編寫接口和實現(xiàn)類
using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.Text;namespace WcfDataSreve {// 注意: 使用“重構(gòu)”菜單上的“重命名”命令,可以同時更改代碼和配置文件中的接口名“IOracleServe”。[ServiceContract][ServiceKnownType(typeof(DBNull))]public interface IOracleServe{[OperationContract]DataTable GetOrderHandleData(string orderindex);[OperationContract]DataTable GetWorkOrderData(string orderindex);[OperationContract]DataTable GetWorkOrderByDate(string starttime, string endtime);[OperationContract]string DeleteOrderHandle(string orderindex);[OperationContract]string DeleteAllOrderHandle(string orderindex);[OperationContract]string StrConn(string index);} }using System; using System.Collections.Generic; using System.Data; using System.Data.OracleClient; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.Text;namespace WcfDataSreve {// 注意: 使用“重構(gòu)”菜單上的“重命名”命令,可以同時更改代碼和配置文件中的類名“OracleServe”。 //OracleServe()中讀取配置文件解析 //IIS中運行的時候以組件的形式添加至WCF服務(wù)網(wǎng)站中,故運行的時候讀取WCF服務(wù)網(wǎng)站中的配置文件(Web.config) public class OracleServe : IOracleServe
{
public string ConnectionString { get; set; }
public OracleServe()
{
ConnectionString = System.Configuration.ConfigurationManager.AppSettings["ConnectionString"];
}public DataTable GetOrderHandleData(string orderindex){using ( OracleConnection conn = new OracleConnection(ConnectionString)){conn.Open();OracleCommand cmd = new OracleCommand();cmd.CommandText = @"select * from M_ORDERHANDLE where ORDERINDEX = '" + orderindex + "'"; cmd.Connection = conn;cmd.CommandType = CommandType.Text;OracleDataAdapter da = new OracleDataAdapter(cmd);DataSet ds = new DataSet();da.Fill(ds,"OrderHandle");conn.Close();return ds.Tables["OrderHandle"];} }public DataTable GetWorkOrderData(string orderindex){using (OracleConnection conn = new OracleConnection(ConnectionString)){conn.Open();OracleCommand cmd = new OracleCommand();cmd.Connection = conn;cmd.CommandText = @"SELECT * FROM M_WORKORDER where ORDERINDEX = '" + orderindex + "'"; cmd.CommandType = CommandType.Text;OracleDataAdapter da = new OracleDataAdapter(cmd);DataSet ds = new DataSet();da.Fill(ds, "WorkOrder");return ds.Tables["WorkOrder"];}}public DataTable GetWorkOrderByDate(string starttime, string endtime ){using (OracleConnection conn = new OracleConnection(ConnectionString)){conn.Open();OracleCommand cmd = new OracleCommand();cmd.Connection = conn;cmd.CommandText = @"SELECT * FROM M_WORKORDER where to_char(CREATETIME, 'yyyy-mm-dd') >= '" + Convert.ToDateTime(starttime).ToString("yyyy-MM-dd") + "' and to_char(CREATETIME, 'yyyy-mm-dd') <= '" + Convert.ToDateTime(endtime).ToString("yyyy-MM-dd") + "'";cmd.CommandType = CommandType.Text;OracleDataAdapter da = new OracleDataAdapter(cmd);DataSet ds = new DataSet();da.Fill(ds, "WorkOrder");return ds.Tables["WorkOrder"];}}public string StrConn(string index){return ConnectionString+index;}public string DeleteOrderHandle(string orderindex){using (OracleConnection conn = new OracleConnection(ConnectionString)){conn.Open();OracleCommand cmd = new OracleCommand();cmd.Connection = conn;cmd.CommandText = @"Delete FROM M_ORDERHANDLE where HANDLELSH = '" + orderindex + "'"; cmd.CommandType = CommandType.Text;if(cmd.ExecuteNonQuery()>0){return "OK";}else{return "No";}}}public string DeleteAllOrderHandle(string orderindex){using (OracleConnection conn = new OracleConnection(ConnectionString)){conn.Open();OracleCommand cmd = new OracleCommand();cmd.Connection = conn;cmd.CommandText = @"Delete FROM M_ORDERHANDLE where ORDERINDEX = '" + orderindex + "'";cmd.CommandType = CommandType.Text;if (cmd.ExecuteNonQuery() > 0){return "OK";}else{return "No";}}}} }
ConnectionString = System.Configuration.ConfigurationManager.AppSettings["ConnectionString"];此句讀取下面創(chuàng)建的WCF服務(wù)網(wǎng)站中web.config的節(jié)點用于獲取數(shù)據(jù)庫連接
4,生成解決方案
5,右擊解決方案添加新建網(wǎng)站-》WCF服務(wù)
6,右擊WCF網(wǎng)站添加引用
7,在彈出框中找到點擊項目在右邊找到剛才創(chuàng)建的類庫組件添加(以下圖片只做演示流程與實際項目不一樣)
(出現(xiàn)類似此圖則成功)
8,若果沒有添加引用冊出現(xiàn)如下圖的錯誤
9,更改網(wǎng)站信息,web.config,添加數(shù)據(jù)庫訪問路徑節(jié)點(加粗部分)(注意如果不能修改傳輸配額以及添加其他節(jié)點信息則可以跳過此步驟選擇默認,
? ? 此處修改傳輸容量和訪問數(shù)據(jù)庫的節(jié)點信息)
<?xml version="1.0" ?> - <configuration> - <system.web><compilation debug="true" targetFramework="4.0" /> </system.web> - <system.serviceModel> - <services> - <service name="WcfDataSreve.OracleServe"><endpoint address="basic" binding="basicHttpBinding" bindingConfiguration="LargeDataTransferServicesBinding" contract="WcfDataSreve.IOracleServe" /> </service></services> - <bindings> - <basicHttpBinding> - <binding name="LargeDataTransferServicesBinding" maxReceivedMessageSize="2147483647" messageEncoding="Text" transferMode="Streamed" sendTimeout="00:10:00"><readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="2147483647" maxBytesPerRead="4096" maxNameTableCharCount="16384" /> </binding></basicHttpBinding></bindings> - <behaviors> - <serviceBehaviors> - <behavior> - <!-- 為避免泄漏元數(shù)據(jù)信息,請在部署前將以下值設(shè)置為 false --> <serviceMetadata httpGetEnabled="true" /> - <!-- 要接收故障異常詳細信息以進行調(diào)試,請將以下值設(shè)置為 true。在部署前設(shè)置為 false 以避免泄漏異常信息 --> <serviceDebug includeExceptionDetailInFaults="false" /> </behavior></serviceBehaviors></behaviors><serviceHostingEnvironment multipleSiteBindingsEnabled="true" /> </system.serviceModel> - <system.webServer><modules runAllManagedModulesForAllRequests="true" /> - <!-- 若要在調(diào)試過程中瀏覽 Web 應用程序根目錄,請將下面的值設(shè)置為 True。在部署之前將該值設(shè)置為 False 可避免泄露 Web 應用程序文件夾信息。--> <directoryBrowse enabled="true" /> </system.webServer> - <appSettings><add key="ConnectionString" value="Data Source=10.192.165.144/OSMP;User ID=cnosm;Password=osm1234$" /> </appSettings></configuration>9.*1,修改配額的數(shù)據(jù)可以參考app.config(類庫中或項目中)
9.*2,修改配額之后也需要在客戶端修改傳輸配額(下面從別的地方選取已做參考)
<system.serviceModel><behaviors /><bindings><basicHttpBinding><binding name="BasicHttpBinding_ISqlService" receiveTimeout="00:05:00"maxBufferSize="2147483647" maxReceivedMessageSize="2147483647" ><readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="2147483647"maxBytesPerRead="4096" maxNameTableCharCount="16384" /></binding></basicHttpBinding></bindings><client><endpoint address="http://localhost:9755/Service.svc/basic"binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_ISqlService"contract="SQLService.ISqlService" name="BasicHttpBinding_ISqlService" /></client></system.serviceModel>解析
BasicHttpBinding_ISqlService這個是添加引用自動生成的 address="http://localhost:9755/Service.svc/basic 和 SQLService.ISqlService 這是二個均可以在app.config中找到, basicHttpBinding這一個是關(guān)于綁定配額用basicHttpBinding綁定LargeDataTransferServicesBinding,LargeDataTransferServicesBinding又配置數(shù)據(jù),
注意客戶端的basinHttpBinding和LargeDataTransferServicesBinding與WCF服務(wù)網(wǎng)站中的配置名字一樣
10,更改WCF服務(wù)下的Service.svc文件,并把App_Code文件夾下的系統(tǒng)定義的服務(wù)刪除
<%@ ServiceHost Language="C#" Debug="true" Service="WcfDataSreve.OracleServe" >
? ? ? ? ? ? ? 加粗部分為類庫名.服務(wù)實現(xiàn)類
11,當傳輸數(shù)據(jù)量大或是需要控制傳輸量時添加加粗部分代碼
<?xml version="1.0"?> <configuration><system.web><compilation debug="true" targetFramework="4.0"/></system.web><system.serviceModel><services><service name="WcfDataSreve.OracleServe"><endpoint address="basic" binding="basicHttpBinding" bindingConfiguration="LargeDataTransferServicesBinding" contract="WcfDataSreve.IOracleServe"/></service></services><bindings><basicHttpBinding><binding name="LargeDataTransferServicesBinding" maxReceivedMessageSize="2147483647"messageEncoding="Text" transferMode="Streamed" sendTimeout="00:10:00" ><readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="2147483647"maxBytesPerRead="4096" maxNameTableCharCount="16384" /></binding></basicHttpBinding></bindings><behaviors><serviceBehaviors><behavior><!-- 為避免泄漏元數(shù)據(jù)信息,請在部署前將以下值設(shè)置為 false --><serviceMetadata httpGetEnabled="true"/><!-- 要接收故障異常詳細信息以進行調(diào)試,請將以下值設(shè)置為 true。在部署前設(shè)置為 false 以避免泄漏異常信息 --><serviceDebug includeExceptionDetailInFaults="false"/></behavior></serviceBehaviors></behaviors><serviceHostingEnvironment multipleSiteBindingsEnabled="true"/></system.serviceModel><system.webServer><modules runAllManagedModulesForAllRequests="true"/><!--若要在調(diào)試過程中瀏覽 Web 應用程序根目錄,請將下面的值設(shè)置為 True。在部署之前將該值設(shè)置為 False 可避免泄露 Web 應用程序文件夾信息。--><directoryBrowse enabled="true"/></system.webServer><appSettings><add key="ConnectionString" value="Data Source=10.192.165.144/OSMP;User ID=cnosm;Password=osm1234$"/></appSettings> </configuration>12,右擊解決方案創(chuàng)建WPF程序引用上面的服務(wù)
?在WPF項目中右擊引用-》添加服務(wù)引用-》打開頁面點擊發(fā)現(xiàn),確認即可,
引用方法是,服務(wù)引用名.服務(wù)實現(xiàn)類名Client new wcf=服務(wù)引用名.服務(wù)實現(xiàn)類名Client();即可通過對象名引用服務(wù)接口(部分代碼)
public class DataViewModel: ViewModelBase{public ObservableCollection<OrderHandle> OrderHandleList{get; set;}public WCFOracleService.OracleServeClient Wcf;public DataViewModel(){OrderHandleList = new ObservableCollection<OrderHandle>();Wcf = new WCFOracleService.OracleServeClient();DeleteSelect = new RelayCommand(ExecuteSelect, CanExecuteSelect);DeleteAll = new RelayCommand(ExecuteAllDelete, CanExecuteALl);}public void QueryOrderHandle(string orderindex){DataTable table = Wcf.GetOrderHandleData(orderindex);if(table==null){return;}for(int i=0;i<table.Rows.Count;i++){OrderHandle Oh = new OrderHandle();Oh.HandleDescrtption = table.Rows[i]["HANDLEDESCRIPTION"].ToString();Oh.HandLelsh = table.Rows[i]["HANDLELSH"].ToString();Oh.OrderIndex = table.Rows[i]["ORDERINDEX"].ToString();Oh.HandlePerson = table.Rows[i]["HANDLEPERSON"].ToString();Oh.HandLetype = table.Rows[i]["HANDLETYPE"].ToString();Oh.HandleTime =table.Rows[i]["HANDLETIME"].ToString();Oh.HandleStatus = table.Rows[i]["HANDLESTATUS"].ToString();Oh.Select = false;OrderHandleList.Add(Oh);}}8 發(fā)布至IIS
? ? ? (1).把整個WCF服務(wù)文件拷貝至服務(wù)器中
? ? ??
(2)找到服務(wù)器中IIS管理器找到網(wǎng)站,新添加一個網(wǎng)站,配置基本信息,
(3)配置完成后,點擊IIS右側(cè)瀏覽網(wǎng)站-》點擊Service.svc,拷貝如下地址到添加服務(wù)引用的頁面即可找到,并引用
?
svcutil.exe http://IP地址和端口號 /Service.svc?wsdl
轉(zhuǎn)載于:https://www.cnblogs.com/wangboke/p/5354635.html
總結(jié)
以上是生活随笔為你收集整理的WCF创建到使用到发布的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java多线程使用场景
- 下一篇: 分布式入门之3:副本控制