我被困在一个小问题上,它停止了我正在尝试实现的功能的开发。
我当前使用以下代码创建了一个 WCF Web 服务:
[ServiceContract(Namespace = "http://Sinvise.Service/")]
public interface ISinvise
{
[OperationContract]
void Output(string value);
}
class SinviseService : ISinvise
{
second sec = new second();
public void Output(string value)
{
sec.message(value);
}
}
主要方法:
var ip = getIP();
Uri baseAddr = new Uri("http://"+ip+":60185/Sinvise");
ServiceHost localHost = new ServiceHost(typeof(SinviseService), baseAddr);
Console.WriteLine("Current System IP: " + getIP());
try
{
Process.Start(baseAddr.AbsoluteUri);
localHost.AddServiceEndpoint(typeof(ISinvise), new BasicHttpBinding(), "SinviseService");
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
localHost.Description.Behaviors.Add(smb);
localHost.Open();
Console.WriteLine("Service initialized.");
Console.WriteLine("Press the ENTER key to terminate service.");
Console.ReadLine();
localHost.Close();
}
catch (CommunicationException ex)
{
Console.WriteLine("Oops! Exception: {0}", ex.Message);
localHost.Abort();
}
应用程序设置要在应用程序中调用的 Web 服务,现在我要做的是使用 HTML 页面来调用操作。它们是将值传递到应用程序的简单操作。
现在我知道 HTML 不能自己做到这一点,PHP 在这方面不是一个选项,因为它需要我在我的应用程序部署中打包一个 Web 服务器和 PHP(我不能这样做,因为我的应用程序需要付费,因此不能包含开源软件)。
我在使用 jQuery 时遇到问题,因为它不能以跨域方式使用。
Web 服务与 ASP.NET 无关,但我需要一些东西来允许我的软件用户能够使用 HTML 页面调用 Web 服务。
谢谢
如果我的理解是正确的,并且基于此:
我在使用 jQuery 时遇到问题,因为它不能以跨域方式使用。
您的服务位于另一个域中,然后以最简单的方式执行此操作,在您的域中创建 REST 服务,然后包装对外部服务的调用
看到这个问题
这是创建 REST 服务的演练(没有 SVC 文件,可以在 ASP.Net 或 MVC 应用中使用)
使用页面方法的数据列表删除命令事件实现
这是一个完整的应用程序,你可以下载,使用REST服务。
http://sdrv.ms/LJJz1K
例:
[ServiceContract]
public interface IMyService
{
[OperationContract]
[WebInvoke(
ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json,
UriTemplate = "/DeleteFromService",
Method = "DELETE")]
void Delete(int id);
}
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class MyService : IMyService
{
public void Delete(int id)
{
// wrap here the call to your external service
// simulate a long process
Thread.Sleep(5000);
}
}
在页面中
<script type="text/javascript" language="javascript">
function deleteFromService() {
$.blockUI();
$.ajax({
cache: false,
type: "DELETE",
async: true,
url: "/DeleteFromService",
data: "3", // get your id to delete
contentType: "application/json",
dataType: "json",
success: function () {
$(document).ajaxStop($.unblockUI);
alert("done");
},
error: function (xhr) {
$(document).ajaxStop($.unblockUI);
alert(xhr.responseText);
}
});
}
jQuery().ready(function () {
$("#myButton").click(deleteFromService);
});
</script>
如果您希望它不需要服务器端代码,则至少需要使用客户端代码。(JavaScript)。
下面是如何使用 JavaScript 直接调用 Web 服务的示例: http://dotnetbyexample.blogspot.com/2007/10/calling-asmx-web-services-directly-from.html
(除了这个结果之外,还有一个简单的搜索。
正如你所说:-
I am having a problem using jQuery as it can not be used in a cross domain manner
但是,这个问题似乎通过使用ScriptManager
[未尝试]得到了解决。请参阅此处分步完整示例
- http://pranayamr.blogspot.co.uk/2011/06/calling-cross-domain-wcf-service-using.html 标题下
Solution 2 : Call WCF Service by Javascript
但是,请参阅这两种解决方案。
如果你现在真的想使用HTML。你应该看看 REST 服务 .查看 WCF 数据服务或 Asp.net WebApi。你可以找到很多教程,这些教程可以帮助你轻松实现服务,并让你在HTML中使用jquery。