pythonwcf調用
A. 請教一個python調用webservice時進行soapheader認證的問題
本文僅提供通過設置SoapHeader來控制非法用戶對WebService的調用,如果是WebService建議使用WSE3.0來保護Web服務,如果使用的是ViaualStudio2008可以使用WCF,WCF裡面提供了的服務認證方法。以下提供一種基於SoapHeader的自定義驗證方式。1.首先要自定義SoapHeader,須繼承System.Web.Services.Protocols.SoapHeader。usingSystem;usingSystem.Collections.Generic;usingSystem.Web;//////自定義的SoapHeader///publicclassMySoapHeader:System.Web.Services.Protocols.SoapHeader{privatestringuserName=string.Empty;privatestringpassWord=string.Empty;//////構造函數///publicMySoapHeader(){}//////構造函數//////用戶名///密碼publicMySoapHeader(stringuserName,stringpassWord){this.userName=userName;this.passWord=passWord;}//////獲取或設置用戶用戶名///publicstringUserName{get{returnuserName;}set{userName=value;}}//////獲取或設置用戶密碼///publicstringPassWord{get{returnpassWord;}set{passWord=value;}}}2.添加WebService,並編寫相應代碼。usingSystem;usingSystem.Collections.Generic;usingSystem.Web;usingSystem.Web.Services;//////WebService的摘要說明///[WebService(Namespace="")][WebServiceBinding(ConformsTo=WsiProfiles.BasicProfile1_1)]publicclassWebService:System.Web.Services.WebService{//聲明Soap頭實例publicMySoapHeadermyHeader=newMySoapHeader();[System.Web.Services.Protocols.SoapHeader("myHeader")][WebMethod]publicstringHelloWord(){//可以通過存儲在資料庫中的用戶與密碼來驗證if(myHeader.UserName.Equals("houlei")&myHeader.PassWord.Equals("houlei")){return"調用服務成功!";}else{return"對不起,您沒有許可權調用此服務!";}}}3.客戶端調用,分別使用不設置SoapHeader與設置SoapHeader。usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespaceApp{classProgram{staticvoidMain(string[]args){localhost.WebServiceservice=newlocalhost.WebService();//沒有設置SoapHeader的服務調用Console.WriteLine("沒有設置SoapHeader:"+service.HelloWord());Console.WriteLine();//將用戶名與密碼存入SoapHeader;localhost.MySoapHeaderheader=newlocalhost.MySoapHeader();header.UserName="houlei";header.PassWord="houlei";service.MySoapHeaderValue=header;//設置SoapHeader的服務調用Console.WriteLine("設置SoapHeader:"+service.HelloWord());Console.Read();}}}4.運行應用程序,查看運行結果。再看一下直接通過瀏覽器的調用結果。點擊HelloWord調用Web服務,結果如下:點擊「調用」按鈕,得到從伺服器返回調用結果。添加自定義SoapHeader可以成功調用WebService,否則不能調用WebService,從而實現對WebService的非法調用。這種方法存在一定的弊端,就是在每一個WebService方法上都要進行一下驗證,如果用戶名與密碼存儲在資料庫中,每調用一次WebService都要訪問一次資料庫進行用戶名與密碼的驗證,對於頻繁調用WebService來說,資料庫壓力很大。然而少量WebService調用這種方式還是一種不錯的選擇
B. 調用WCF服務端方法,方法執行時間過長,怎麼處理比較合理
加速演算法!
多線程!
C. 求教調用WCF超時問題的解決辦法
在 Silverlight 端設置 SendTimeout 和 ReceiveTimeout 屬性,經常在 Debug 時需要調試上好幾分鍾、或者放上幾十分鍾。
可以在 ServiceReferences.ClientConfig 中配置,不過個人習慣使用後台代碼,畢竟配置最靈活:
BasicHttpBinding 在 Debug/ Release 時的代碼配置
C# code
defaultBasicHttpBinding = new BasicHttpBinding(BasicHttpSecurityMode.None)
{
Name = "DefaultBasicHttpBinding",
OpenTimeout = new TimeSpan(0, 1, 0),
CloseTimeout = new TimeSpan(0, 1, 0),
#if DEBUG
SendTimeout = new TimeSpan(0, 18, 00),
ReceiveTimeout = new TimeSpan(18, 18, 00),
MaxBufferSize = int.MaxValue,
MaxReceivedMessageSize = int.MaxValue
#else
SendTimeout = new TimeSpan(0, 8, 00),
ReceiveTimeout = new TimeSpan(0, 18, 00),
MaxBufferSize = int.MaxValue,
MaxReceivedMessageSize = int.MaxValue
#endif
};
CustomBinding
C# code
customBinding = new CustomBinding()
{
Name = "CustomBinding",
OpenTimeout = new TimeSpan(0, 1, 0),
CloseTimeout = new TimeSpan(0, 1, 0),
#if DEBUG
SendTimeout = new TimeSpan(0, 18, 00),
ReceiveTimeout = new TimeSpan(18, 18, 00),
#else
SendTimeout = new TimeSpan(0, 8, 00),
ReceiveTimeout = new TimeSpan(0, 18, 00),
#endif
};
customBinding.Elements.Add(new System.ServiceModel.Channels.());
customBinding.Elements.Add(new HttpTransportBindingElement()
{
MaxBufferSize = int.MaxValue, MaxReceivedMessageSize= int.MaxValue
}
);
D. WCF客戶端調用服務端帶有Hashtable為參數的方法報錯
Hashtable不支持集合序列化,所以會自動被轉換,你可以修改你的服務契約
int UpdFirm(Dictionary<string, object> dic, string firmID);
Dictionary<string,object>dic=newDictionary<string,object>();
dic.Add("姓名","小明");
dic.Add("性別","男");
fsc.UpdFirm(dic,"001");
E. WCF服務頭一次,第一次調用慢的問題求助
重新生成服務,右鍵svc在瀏覽器中查看 復制步驟1中的網址,在使用項目裡面添加服務引用,粘貼網址,前往,確定。注意觀察命名空間之類的 在要使用的頁面引用命名空間,new client,然後就有各種方法可以調用。
F. WCF調用時怎麼在請求中加自定義的頭
用 BeforeNavigate2() 事件,其 Header 參數使您能夠向傳出事務插入任意標頭。但是,因為此事件是對於主 URL 觸發的,對於其任何內嵌內容並不能觸發,所以對於圖像、腳本文件等不會收到此事件。
有一種肯定能夠觸發的方法可用於向每個傳出事務添加數據。假設您安裝了一個自定義軟體程序包(可能具有一些 ActiveX® 控制項宿主於您站點之外的網頁上),您希望將這種情況以信號的形式發送給伺服器,而不管用戶訪問您站點的方式是使用 Internet Explorer 還是使用 WebBrowser 主機。您可以通過向下列注冊表項插入一個新的 String 值,將這些信息附加到 Internet Explorer User-Agent 字元串的最後:
\\HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentControlSet\
Internet Settings\5.0\User Agent\Post Platform!
G. wcf web服務用什麼調用方法
1. 創建服務
2. 修改介面
為了做演示,我們將默認的那個Operation修改一下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ServiceModel.Web;
namespace WebApplication1
{
// 注意: 如果更改此處的介面名稱 "INorthwindService",也必須更新 Web.config 中對 "INorthwindService" 的引用。
[ServiceContract]
public interface INorthwindService
{
[OperationContract]
[WebGet(UriTemplate="HelloWorld")]
string HelloWorld();
}
}
注意,我們這里加了一個WebGet的Attribute,這將允許WCF服務直接通過地址調用
3. 實現服務
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace WebApplication1
{
// 注意: 如果更改此處的類名 "NorthwindService",也必須更新 Web.config 中對 "NorthwindService" 的引用。
public class NorthwindService : INorthwindService
{
#region INorthwindService 成員
public string HelloWorld()
{
return "Hello,world";
}
#endregion
}
}
這里的實現依然是我最喜歡的HelloWorld
4. 修改配置文件(web.config),要支持直接通過WebGet的方法調用WCF服務,必須用一個特殊的binding,是webHttpBinding
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="WebApplication1.NorthwindServiceBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug ="false" />
behavior>
serviceBehaviors>
<endpointBehaviors>
<behavior name="test">
<webHttp/>
behavior>
endpointBehaviors>
behaviors>
<services>
<service behaviorConfiguration="WebApplication1.NorthwindServiceBehavior"
name="WebApplication1.NorthwindService">
<endpoint address="" binding="webHttpBinding" contract="WebApplication1.INorthwindService" behaviorConfiguration="test">
<identity>
<dns value="localhost" />
identity>
endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
service>
services>
system.serviceModel>
上面的粗斜體部分是要添加或者修改的
H. C#調用WCF服務的方法
跟webservice一樣, 先引用那個服務, 在代碼里直接調用就行
ServiceClient client = new ServiceClient();
using (Stream fileStream = new FileStream("File Name", FileMode.Open, FileAccess.Read, FileShare.Read))
{
client.UploadFile("Name", DateTime.Today.ToString("yyyyMMddhhmmss"), "ID", fileStream);
fileStream.Close();
}
我這個是一個上傳服務的調用方式
I. 調用WCF時提示無法激活請求的服務。是怎麼回事呀
運行如下命令:
1
appcmd set app "localhost/MyService" /enabledProtocols:http,net.tcp
更改應用程序設置。
然後重啟試試呢?
J. wcf 調用問題 1.通過vs直接引用服務;2.通過vs命令工具生成代理類; 求大神告知這兩方式有什麼區別,利弊
WCF服務調用通過兩種常用的方式:一種是藉助代碼生成工具SvcUtil.exe或者添加服務引用的方式,一種是通過ChannelFactory直接創建服務代理對象進行服務調用。
下面簡單說下如何通過SvcUtil.exe的方式調用服務:
SvcUtil.exe是一個命令行工具,位於:C:\Program Files\Microsoft SDKs\Windows\v7.0A\bin目錄下,可以將SvcUtil.exe添加到VS2010中方便以後的運用,添加方式:在VS2010的Tools菜單中選擇External Tools
點擊添加按鈕,在Title欄輸入SvcUtil,Command欄選擇SvcUtil.exe所在的目錄,Initial directory欄選擇生成的客戶端代碼和配置文件所放的目錄,選上Prompt for arguments,不選上Close on exit,點擊OK完成添加。
添加好以後,點擊VS2010菜單下的SvcUtil,輸入提供元數據的服務終結點的 URL
點擊OK,在彈出的命令行中會顯示成功與否和相關信息。
如果成功的話會生成兩個文件,一個是包含了服務端Contract和對應於服務端service的本地代理類的cs文件,一個是WCF配置文件,將這兩個文件拷到項目中並將output.config改名為app.config就可以了。