让我们说在一个ASP.NET应用程序,WCF或Web API中,这个应用程序工作的一部分是在途中联系第三方.我喜欢异步或非阻塞地执行此操作,以便线程池不会饿死.但是,我不想在服务中更改我的所有代码,只需要进行Web调用.
这是我写的一些代码:
public string GetSomeData()
{
Task<string> stuff = CallApiAsync();
return stuff.result; //does this block here?
}
private async Task<string> CallApiasync()
{
using (var httpClient = new HttpClient())
{
string response = await httpClient.GetStringAsync(Util.EndPoint).ConfigureAwait(false);
return response;
}
}
Run Code Online (Sandbox Code Playgroud)
我认为这个想法如下,但请纠正任何误解.
CallApi的调用者可以调用该方法,当它等待等待时,会创建一个Task,它代表一些异步完成的工作,但这需要一些时间.此时线程到达等待返回到线程池以执行其他操作,即处理不同的请求.一旦任务完成,await线就会唤醒并且代码从那里继续,就好像它是同步的一样.
如果是这种情况,为什么我需要从我的apimethod返回一个任务.调用者似乎必须调用stuff.Result,这意味着任务可能没有完成,调用结果可能会阻塞?注意我不想让调用方法异步,因为调用它的方法需要是异步等等.
我的代码中的事件顺序是什么?
另一个问题是为什么我需要将configureAwait设置为false?否则一切都会挂起.
我正在使用WCF服务(启用Ajax).在我的服务中,当发生错误时,我将其记录在数据库中,并且我将用户重定向到错误页面.
要重定向用户,我使用此:
HttpContext.Current.Response.Redirect("error.aspx");
Run Code Online (Sandbox Code Playgroud)
所以我正在记录错误并重定向用户
问题是,当发生错误时,会多次记录该错误,并且不会重定向用户.
当我说它被多次记录时,它意味着:我的数据库中有多个错误记录用于此错误.(仅假设一个错误有一个日志)
所以我试着理解:为什么?
看起来当我尝试重定向时,服务方法再次开始(就像它再次调用一样).
这是我的完整流程:
我用jquery调用服务:
$.getJSON("ContactService.svc/getsubtitle", { titleid: titleid }, function (data) {
...
});
Run Code Online (Sandbox Code Playgroud)
这是服务方法:
[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Json)]
public void getsubtitle(string titleid)
{
try
{
.... (ERROR !)
}
catch (Exception ex)
{
LogTheError(ex);
HttpContext.Current.Response.Redirect("error.aspx");
}
}
Run Code Online (Sandbox Code Playgroud)
所以,你们能帮助我理解为什么HttpContext.Current.Response.Redirect不重定向并做一件奇怪的事情吗?
我想将log4net用于IIS中托管的WCF服务.
但是要轻松更改设置,我想使用单独的配置文件.
所以我添加到Web.Config(以及WCF服务库的App.config)
<appSettings>
<add key="log4net_config" value="log4net.config" />
</appSettings>
Run Code Online (Sandbox Code Playgroud)
但是这会导致IIS的当前目录
C:\Windows\System32\inetsrv
Run Code Online (Sandbox Code Playgroud)
并且永远不会有我的log4net.config文件.
但我想配置log4net之类的东西
var configFile = ConfigurationManager.AppSettings["log4net_config"];
var fileInfo = new FileInfo(configFile);
XmlConfigurator.ConfigureAndWatch(fileInfo);
Run Code Online (Sandbox Code Playgroud)
如何配置满足此需求的目录?
我的目标是通过IIS将SQL Server 2008表公开为XML/JSON.
我按照几个(1) 视频(2)来展示WCF数据服务Web应用程序,当$ metadata页面工作时,尝试查看实际数据会导致异常:
Operation could destabilize the runtime.
System.Security.VerificationException
Run Code Online (Sandbox Code Playgroud)
我从这个带有.NET 4.5.1和Entity Framework 6.1.2的VS2013 模板开始- 下面是更具体的软件包版本.
如果NuGet包导致VerificationException,我应该尝试删除所有NuGet包,并只添加EntityFramework(以及依赖的任何东西)?
欣赏任何想法,谢谢你们!
Id Version Description/Release Notes
-- ------- -------------------------
Antlr 3.5.0.2 ANother Tool for Language Recognition, is a language tool that provides a framework for constructing recognizers, in...
bootstrap 3.3.2 Sleek, intuitive, and powerful mobile first front-end framework for faster and easier web development.
EntityFramework 6.1.2 Entity Framework is Microsoft's recommended data access technology for new applications.
jQuery …Run Code Online (Sandbox Code Playgroud) 我一直在评估Nathanael Jones 令人惊叹的成像库和插件,用于我公司在Azure上构建的一些图像处理服务.在获得许可证之前,我们会对它们进行全面测试,以确保它们符合我们的方案.帮自己一个忙,在这里检查一下.
在ASP.NET MVC Web应用程序中使用插件时,我在插件方面取得了巨大成功.我正在使用我在UI中发布的Controller中的图像服务器功能.裁剪,调整大小和简单/高级过滤器正在按预期工作.
我遇到的问题是当我将此功能作为该应用程序中的类库移动到WCF服务时.裁剪和调整大小的工作完全符合预期,但所有过滤指令(亮度,对比度,棕褐色等)都被忽略或无声地失败.这是图像处理代码:
var instructions = new ImageResizer.Instructions();
//All of these instructions work
instructions.Width = 300;
instructions.Height = 300;
instructions.Mode = ImageResizer.FitMode.Crop;
instructions.OutputFormat = ImageResizer.OutputFormat.Jpeg;
instructions.JpegQuality = 90;
double[] cropCoordinates = {0,100,0,100};
instructions.CropRectangle = cropCoordinates;
instructions.Mode = ImageResizer.FitMode.Crop;
//These instructions are ignored, or fail silently
instructions.Invert = true;
instructions.Saturation = -1;
instructions.Sepia = true;
var imageJob = new ImageResizer.ImageJob();
imageJob.Instructions = instructions;
imageJob.Source = bmpSource;
imageJob.Dest = typeof(Bitmap);
imageJob.Build();
Run Code Online (Sandbox Code Playgroud)
我已经将我的MVC应用程序使用的Web.Config设置复制到使用ImageResizing包(来自Nuget)的类库的App.Config.
<configuration>
<configSections>
<section …Run Code Online (Sandbox Code Playgroud) 我正在自定义HTTP模块中编写授权功能.这是我的代码:
private bool CheckAuthorization(HttpContext context)
{
string auth = context.Request.Headers["Authorization"];
if (string.IsNullOrEmpty(auth) || auth != "123")
{
context.Response.StatusCode = -404;//HttpStatusCode.MethodNotAllowed;
context.Response.Write("404 Not allowed!");
//webOperationContext.OutgoingResponse.StatusCode = HttpStatusCode.MethodNotAllowed;
}
return true;
}
Run Code Online (Sandbox Code Playgroud)
我的开发环境是:C#+ .NET Framwork 4.0 + Visual Studio 2013 + WCF.我的目的是:当检查授权失败时,请求不应该调用WCF方法,并返回404方法不允许错误.它仍然调用WCF方法现在通过返回错误提示.谢谢!
我已经创建了WCF服务,并且Web.Config文件具有以下设置。
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647" messageEncoding="Text" sendTimeout="10:00:00" openTimeout="10:00:00">
<readerQuotas maxDepth="2000000" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
</binding>
</basicHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information, set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="Axis.OptimizationService.RMSCalculationService">
<endpoint address="" binding="basicHttpBinding" …Run Code Online (Sandbox Code Playgroud) 如何限制文本框中的特殊字符和字符?我正在使用此代码,但我并不限制特殊字符和字符
代码: -
if (!Regex.IsMatch(((Windows.UI.Xaml.Controls.TextBox)sender).Text, @"^\\d*\\.?\\d*$"))
{
// Write Code
}
Run Code Online (Sandbox Code Playgroud) 我需要submitOut从 async Task testWCF2下面的函数返回值吗?任何人都可以提供如何操作的指导吗?
public static async Task testWCF2(string xmlConfig)
{
string submitOut;
using (var client = new System.Net.Http.HttpClient())
{
var url = "http://server:8100/api/SoftwareProductBuild";
var content = new StringContent(xmlConfig, Encoding.UTF8, "application/xml");
var response = await client.PostAsync(url, content);
if (response.IsSuccessStatusCode)
{
var responseBody = await response.Content.ReadAsStringAsync();
submitOut = responseBody;
}
else
{
submitOut = string.Format("Bad Response {0} \n", response.StatusCode.ToString());
submitOut = submitOut + response;
}
}
}
public string QlasrSubmit(List<XMLSiInfo> xmlConfigs)
{
string submitOut = "QLASR: ";
foreach …Run Code Online (Sandbox Code Playgroud) 我们有两种不同的微服务客户服务和订单服务.客户服务商店有关客户的信息,即名称,DOB等.订单服务将管理客户所下的订单,即订单号,成本等.这是将客户唯一参考/ ID传递给订购服务的最佳方式.
解决方案1:客户ID是客户服务中唯一的GUID.这将传递给订单服务
解决方案2:在客户服务中生成业务/人员友好的唯一代码,并将其传递给订单服务
解决方案3:还有别的吗?
wcf ×10
c# ×7
asp.net ×6
.net ×2
async-await ×2
asp.net-mvc ×1
asynchronous ×1
endpoint ×1
iis ×1
imageresizer ×1
jquery ×1
log4net ×1
redirect ×1
rest ×1
uwp ×1
wcf-binding ×1