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就可以了。