in class library project use WebServiceHandlerFactory to create Web service, http://www.codeproject.com/useritems/wsinaclasslibrary.asp
write a class WebServiceBase to implements webserviceHandlerFactory, then implement your web service inherit from this class, and copy this compoment into your web app bin directory, then add <add path="WSTest.asmx" verb="*" type="WSLibrary.WSTest" validate="false"/> into your web.config's httphandler section.
[WebMethod(EnableSession=true)]XXX 此声明可以在WS中引用session对象, 某种程度上可以提高性能; 但需要客户端在调用时支持,默认情况下browser中调用WS时要有SESSION的,而在普通的EXE或程序中调用WS时,需要显示声明要使用cookie,否则服务器端的session无效..net 中声明如下:
WebService1 srv = new WebService1();
srv.CookieContainer = new CookieContainer();
只有经过这样声明,调用的WS才会传递SESSIONID,建立在一个应用中只有一个地方可获取WS,即采用singleton模式,这样所有地方调用WS时都有sessionID支持.
利用WS上传文件,可以在CLIENT中将文件中的内容读取到byte[]中,然后调用WS接口,然后WS再将byte写入到文件中
转换FileStream fileStream = File.OpenRead(path + fileName);为MemoryStream,再转换为byte[].
public byte[] ConvertStreamToByteBuffer(System.IO.Stream theStream)
{
int b1;
System.IO.MemoryStream tempStream = new System.IO.MemoryStream();
while ((b1 = theStream.ReadByte()) != -1)
{
tempStream.WriteByte(((byte)b1));
}
return tempStream.ToArray();
}
如果是将byte[]写入到文件,则代码如下:
{
MemoryStream memoryStream = new MemoryStream(fs);
FileStream fileStream = new FileStream(path + fileName, FileMode.Create);
memoryStream.WriteTo(fileStream);
memoryStream.Close();
fileStream.Close();
fileStream = null;
memoryStream = null;