专注于互联网--专注于架构

最新标签
网站地图
文章索引
Rss订阅

首页 »办公软件 » webservice学习:SharePoint(WSS)学习(7) WSS中使用Web Service »正文

webservice学习:SharePoint(WSS)学习(7) WSS中使用Web Service

来源: 发布时间:星期日, 2009年2月8日 浏览:43次 评论:0
  本文举例源代码或素材下载

  由于需要在WSS中使用Web Service经过摸索终于找到比较合适思路方法来开发部署Web Service

  1.建立Asmx

  新建SharePo Empty项目添加Module添加ASMX文件编写ASMX后台代码

  如:TestAsmx.asmx

<%@ WebService Language="C#" Class="WebPartWithAscx.TestAsmx,SampleAsmx, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9f4da00116c38ec5" %>
  
TestAsmx.asmx.cs:
  
using .Web.Services;
  
WebPartWithAscx
{
  [WebService(Namespace = "http://tempuri.org/")]
  [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
  [.ComponentModel.ToolboxItem(false)]
  public TestAsmx : .Web.Services.WebService
  {
  
    [WebMethod]
    public HelloWorld
    {
       "Hello World";
    }
  }
}


  然后修改设置Module.xml关键是设置URL:

<?xml version="1.0" encoding="utf-8"?>
<Elements Id="a7d06f55-3330-46cb-9c87-8c9481fc0047" xmlns="http://schemas.microsoft.com/sharepo/">
 <Module Name="SampleAsmx" Url="UserWebServices">
  <File Path="TestAsmx.asmx" Url="TestAsmx.asmx" />
 </Module>
</Elements>


  编译部署发现部署失败信息为:The file you are attempting to save or retrieve has been blocked from this Web site by the server administrators.

  原因是asmx文件类型被禁止

  打开Central Administration -> Operations -> Blocked file types把列表中asmx文件类型删掉保存可正常部署运行asmx用wsdl命令行生成该asmx代理类加到该Module中(注意:WebService定义和代理类通常情况下是分离在这里为演示将他们放到起;WebService 代理类URL应该能够配置可写在Web.config中本文从略)注意设置命名空间不要和asmx定义冲突编译部署

  2.测试Asmx

  在解决方案中添加WebPart项目

  编写WebService代码部署运行WebPart发现出现:wss The request failed with HTTP status 401: Unauthorized.

  原因是没有权限访问该WebService为WebService添加当前Credentials:

  asmx.Credentials = .Net.CredentialCache.DefaultCredentials;

  再次部署能够正确运行TestSampleAsmx.cs最终代码为:

using ;
using .Runtime.InteropServices;
using .Web.UI.WebControls;
  
TestSampleAsmx
{
  [Guid("a9a79045-ee68-45de-ab33-174fa175f9c9")]
  public TestSampleAsmx : .Web.UI.WebControls.WebParts.WebPart
  {
    Label lbl = null;
    Button btn = null;
  
    public TestSampleAsmx
    {
    }
  
    protected override void CreateChildControls
    {
      btn = Button;
      btn.Text = "Call Web Service";
      btn.Click EventHandler(btn_Click);
  
      lbl = Label;
  
      this.Controls.Add(btn);
      this.Controls.Add(lbl);
    }
  
    void btn_Click(object sender, EventArgs e)
    {
      try
      {
        Xianfen.Net.Asmx.TestAsmx asmx = Xianfen.Net.Asmx.TestAsmx;
        asmx.Credentials = .Net.CredentialCache.DefaultCredentials;
        lbl.Text = asmx.HelloWorld;
      }
      catch (Exception ex)
      {
        lbl.Text = ex.Message;
      }
    }
  }
}




  最终项目视图为:

SharePo<img src='/icons/74098int.gif' />(WSS)学习(7) WSS中使用Web ServiceSharePo<img src='/icons/74098int.gif' />(WSS)学习(7) WSS中使用Web Service

  添加WebPart运行后如图:

SharePo<img src='/icons/74098int.gif' />(WSS)学习(7) WSS中使用Web Service

  飘遥Blog:http://www.cnblogs.com/zxjay/



0

相关文章

读者评论

发表评论

  • 昵称:
  • 内容: