#StackBounty: #c# #.net #wpf #system.drawing #wic Using WIC to Quickly Create Scaled Down Bitmap that honors EXIF Orientation Tag

Bounty: 100

I’m looking for the fastest way to create scaled down bitmap that honors EXIF orientation tag

Ref :https://weblogs.asp.net/bleroy/the-fastest-way-to-resize-images-from-asp-net-and-it-s-more-supported-ish

Currently i use the following code to create a Bitmap that honors EXIF Orientation tag

  static Bitmap FixImageOrientation(Bitmap srce)
        {
            const int ExifOrientationId = 0x112;
            // Read orientation tag
            if (!srce.PropertyIdList.Contains(ExifOrientationId)) return srce;
            var prop = srce.GetPropertyItem(ExifOrientationId);
            var orient = BitConverter.ToInt16(prop.Value, 0);
            // Force value to 1
            prop.Value = BitConverter.GetBytes((short)1);
            srce.SetPropertyItem(prop);

            // Rotate/flip image according to <orient>
            switch (orient)
            {
                case 1:
                    srce.RotateFlip(RotateFlipType.RotateNoneFlipNone);
                    return srce;


                case 2:
                    srce.RotateFlip(RotateFlipType.RotateNoneFlipX);
                    return srce;

                case 3:
                    srce.RotateFlip(RotateFlipType.Rotate180FlipNone);
                    return srce;

                case 4:
                    srce.RotateFlip(RotateFlipType.Rotate180FlipX);
                    return srce;

                case 5:
                    srce.RotateFlip(RotateFlipType.Rotate90FlipX);
                    return srce;

                case 6:
                    srce.RotateFlip(RotateFlipType.Rotate90FlipNone);
                    return srce;

                case 7:
                    srce.RotateFlip(RotateFlipType.Rotate270FlipX);
                    return srce;

                case 8:
                    srce.RotateFlip(RotateFlipType.Rotate270FlipNone);
                    return srce;

                default:
                    srce.RotateFlip(RotateFlipType.RotateNoneFlipNone);
                    return srce;
            }
        }

I’m first creating a orientation fixed image ,then resizing it (preserving aspect ratio) for fast processing.

  public static Bitmap UpdatedResizeImage(Bitmap source, Size size)
        {
            var scale = Math.Min(size.Width / (double)source.Width, size.Height / (double)source.Height);
            var bmp = new Bitmap((int)(source.Width * scale), (int)(source.Height * scale));

            using (var graph = Graphics.FromImage(bmp))
            {
                graph.InterpolationMode = InterpolationMode.High;
                graph.CompositingQuality = CompositingQuality.HighQuality;
                graph.SmoothingMode = SmoothingMode.AntiAlias;
                graph.DrawImage(source, 0, 0, bmp.Width, bmp.Height);
            }
            return bmp;
        }

Now WIC allows much faster image manipulation.Ref:https://stackoverflow.com/a/57987315/848968

How can i create a Scaled Down BitmapImage that honours the EXIF tag

Update:

if ((bitmapMetadata != null) && (bitmapMetadata.ContainsQuery("System.Photo.Orientation")))
            {
                object o = bitmapMetadata.GetQuery("System.Photo.Orientation");

                if (o != null)
                {
                    switch ((ushort)o)
                    {
                        case 3:
                            rotatedImage = new TransformedBitmap(resized, new RotateTransform(180));
                            break;
                        case 6:
                            rotatedImage = new TransformedBitmap(resized, new RotateTransform(90));
                            break;
                        case 8:
                            rotatedImage = new TransformedBitmap(resized, new RotateTransform(270));
                            break;

                    }

                }
            }


Get this bounty!!!

#StackBounty: #c# #.net #asp.net-mvc #model Build a Layout ViewModel based in a Id from the URL with MVC

Bounty: 50

I need to build a ViewModel for my Layout’s Web Application, I have tried this solution but it’s not based on a URL’s Id coming from the URL to generate the Layout’s ViewModel.

I tried this but I first had the not existing empty controller error, then I tried to include the id as a parameter but I get the error “Object reference not set to an instance of an object.” because the LayoutColorRGB is not set.

public MobileController(int id)
{
    Event model = db.Events.Where(s => s.Id == id).FirstOrDefault();

    LayoutVM = new LayoutVM()
    {
        EventId = model.Id,
        LayoutColorRGB = model.LayoutColorRGB,
        SponsorLogoLink = model.SponsorLogoLink,
        SponsorLogoURL = model.SponsorLogoURL
    };

    ViewData["LayoutVM"] = LayoutVM;
}


Get this bounty!!!

#StackBounty: #c# #asp.net #.net #asp.net-mvc #razor MVC Render section scripts in controller

Bounty: 50

I would like to render the “scripts” section of the .cshtml view inside the controller as a string. Is this possible?

What I actually want to do is get the scripts with a separate ajax call and then run eval on the script after loading the html of the view also with ajax.

I’ve tried looking for related topics but haven’t come up with anything relevant. Some related answers fool around with HtmlHelper.ViewContext.HttpContext.Items[""] but I’m unsure how to use it.

What I want is something like this: string scripts = GetScriptsForView(action, controller); which returns the section used in the view like this: @section Scripts {

To clarify (edit):

I’m trying to replace the “RenderBody()” of the layout page with ajax calls, so that I don’t have to load the layout containing the static header every time.

I have managed to replace all <a>-tags with ajax calls replacing the <div> containing the view, but am unable to get the javascripts working.

I could remove the @section scripts { from the cshtml-files and let the script tag get loaded with the html view. The problem with this is that if I reload the page it calls the scripts for that view before calling the scripts of the layout page, resulting in errors. Therefore I wish to load the scripts separately.


Get this bounty!!!

#StackBounty: #c# #.net #unit-testing #library #fluent-assertions Open source project to ease the Assert part from the functional tests…

Bounty: 300

I have built an open-source project based on FluentAssertions in order to solve some recurring tasks I was doing while testing the .Net Core APIs I’m developing.

I find the capability of doing in memory end-to-end tests of the APIs I build a great addition in terms of productivity and validity.

With this project I want to solve two problems: to have a Assert API oriented for HttpResponseMessage, the type of response we get when we do an HTTP request with HttpClient and to get immediate feedback about what went wrong when the test fails, by providing the necessary details in the Test Explorer window from Visual Studio, so I can spend less time in debugging.

I choose FluentAssertions because it treats extensibility as a feature, so they have a great API for doing so. Also, they provide a mechanism to build your own failure messages and link them to a specific type, which is exactly what I needed.

The project is located here.

I have prepared a branch for codereview.stackexchange.com to comply with the rule of not changing the code while you are reviewing it. Hopefully, this is OK, otherwise please let me know how I should proceed.

The nuget package built from this version is

Install-Package FluentAssertions.Web -Version 1.0.113

Next is an example of how it can be used

using FluentAssertions;
using Microsoft.AspNetCore.Mvc.Testing;
using Sample.Api.Net30.Controllers;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Xunit;

namespace Sample.Api.Net30.Tests
{
    public class CommentsControllerTests : IClassFixture<WebApplicationFactory<Startup>>
    {
        private readonly WebApplicationFactory<Startup> _factory;

        public CommentsControllerTests(WebApplicationFactory<Startup> factory)
        {
            _factory = factory;
        }

        [Fact]
        public async Task Get_Returns_Ok_With_CommentsList()
        {
            // Arrange
            var client = _factory.CreateClient();

            // Act
            var response = await client.GetAsync("/api/comments");

            // Assert
            response.Should().Be200Ok().And.BeAs(new[]
            {
                new { Author = "Adrian", Content = "Hey" },
                new { Author = "Johnny", Content = "Hey!" }
            });
        }

        [Fact]
        public async Task Get_WithCommentId_Returns_Ok_With_The_Expected_Comment()
        {
            // Arrange
            var client = _factory.CreateClient();

            // Act
            var response = await client.GetAsync("/api/comments/1");

            // Assert
            response.Should().Be200Ok().And.BeAs(new
            {
                Author = "Adrian",
                Content = "Hey"
            });
        }

        [Fact]
        public async Task Get_Returns_Ok_With_CommentsList_With_TwoUniqueComments()
        {
            // Arrange
            var client = _factory.CreateClient();

            // Act
            var response = await client.GetAsync("/api/comments");

            // Assert
            response.Should().Satisfy<IReadOnlyCollection<Comment>>(
                    model =>
                    {
                        model.Should().HaveCount(2);
                        model.Should().OnlyHaveUniqueItems(c => c.CommentId);
                    }
                );
        }

        [Fact]
        public async Task Get_WithCommentId_Returns_A_NonSpam_Comment()
        {
            // Arrange
            var client = _factory.CreateClient();

            // Act
            var response = await client.GetAsync("/api/comments/1");

            // Assert
            response.Should().Satisfy(givenModelStructure: new
            {
                Author = default(string),
                Content = default(string)
            }, assertion: model =>
                {
                    model.Author.Should().NotBe("I DO SPAM!");
                    model.Content.Should().NotContain("BUY MORE");
                });
        }

        [Fact]
        public async Task Get_WithCommentId_Returns_Response_That_Satisfies_Several_Assertions()
        {
            // Arrange
            var client = _factory.CreateClient();

            // Act
            var response = await client.GetAsync("/api/comments/1");

            // Assert
            response.Should().Satisfy(
                    r =>
                    {
                        r.Content.Headers.ContentRange.Should().BeNull();
                        r.Content.Headers.Allow.Should().NotBeNull();
                    }
            );
        }

        [Fact]
        public async Task Post_ReturnsOk()
        {
            // Arrange
            var client = _factory.CreateClient();

            // Act
            var response = await client.PostAsync("/api/comments", new StringContent(@"{
                      ""author"": ""John"",
                      ""content"": ""Hey, you...""
                    }", Encoding.UTF8, "application/json"));

            // Assert
            response.Should().Be200Ok();
        }

        [Fact]
        public async Task Post_ReturnsOkAndWithContent()
        {
            // Arrange
            var client = _factory.CreateClient();

            // Act
            var response = await client.PostAsync("/api/comments", new StringContent(@"{
                      ""author"": ""John"",
                      ""content"": ""Hey, you...""
                    }", Encoding.UTF8, "application/json"));

            // Assert
            response.Should().Be200Ok().And.BeAs(new
            {
                Author = "John",
                Content = "Hey, you..."
            });
        }

        [Fact]
        public async Task Post_WithNoContent_ReturnsBadRequest()
        {
            // Arrange
            var client = _factory.CreateClient();

            // Act
            var response = await client.PostAsync("/api/comments", new StringContent("", Encoding.UTF8, "application/json"));

            // Assert
            response.Should().Be400BadRequest()
                .And.HaveErrorMessage("*The input does not contain any JSON tokens*");
        }

        [Fact]
        public async Task Post_WithNoAuthorAndNoContent_ReturnsBadRequest()
        {
            // Arrange
            var client = _factory.CreateClient();

            // Act
            var response = await client.PostAsync("/api/comments", new StringContent(@"{
                        ""author"": """",
                        ""content"": """"
                    }", Encoding.UTF8, "application/json"));

            // Assert
            response.Should().Be400BadRequest()
                .And.HaveError("Author", "The Author field is required.")
                .And.HaveError("Content", "The Content field is required.");
        }

        [Fact]
        public async Task Post_WithNoAuthor_ReturnsBadRequestWithUsefulMessage()
        {
            // Arrange
            var client = _factory.CreateClient();

            // Act
            var response = await client.PostAsync("/api/comments", new StringContent(@"{
                                          ""content"": ""Hey, you...""
                                        }", Encoding.UTF8, "application/json"));

            // Assert
            response.Should().Be400BadRequest()
                .And.HaveError("Author", "The Author field is required.")
                .And.NotHaveError("content");
        }

        [Fact]
        public async Task Post_WithNoAuthorButWithContent_ReturnsBadRequestWithAnErrorMessageRelatedToAuthorOnly()
        {
            // Arrange
            var client = _factory.CreateClient();

            // Act
            var response = await client.PostAsync("/api/comments", new StringContent(@"{
                                          ""content"": ""Hey, you...""
                                        }", Encoding.UTF8, "application/json"));

            // Assert
            response.Should().Be400BadRequest()
                .And.OnlyHaveError("Author", "The Author field is required.");
        }
    }
}

And the controller that handles the HTTP requests and the corresponding model looks like:

using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace Sample.Api.Net30.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class CommentsController : ControllerBase
    {
        [HttpGet]
        public IEnumerable<Comment> Get() => new[]
        {
            new Comment { Author = "Adrian", Content = "Hey", CommentId = 1},
            new Comment { Author = "Johnny", Content = "Hey!", CommentId = 2 }
        };

        [HttpGet("{id}")]
        public Comment Get(int id) => new Comment { Author = "Adrian", Content = "Hey", CommentId = id };

        [HttpPost]
        public Comment Post([FromBody] Comment value) => value;
    }

    public class Comment
    {
        [Required]
        public string Author { get; set; }
        [Required, StringLength(maximumLength: 500)]
        public string Content { get; set; }
        public int CommentId { get; set; }
    }
}

The assertion API aims to move the burden of remembering/typing the HTTP status codes as you have the option of recalling either the name or the number. Another common task is to deserialize the response to a certain model type and then to assert on.

The other feature is to show as much information as possible from the request-response pair, when a test fails. So in this example, for the following test, I will get the subsequent messages.

[Fact]
public async Task Get_Returns_Ok_With_CommentsList()
{
    // Arrange
    var client = _factory.CreateClient();

    // Act
    var response = await client.GetAsync("/api/comments");

    // Assert
    response.Should().Be200Ok().And.BeAs(new[]
    {
        new { Author = "Adrian", Content = "Hey" },
        new { Author = "Johnny", Content = "Hey!" },
        new { Author = "John", Content = "Hey!" }
    });
}

this test fails so the message is

Sample.Api.Net30.Tests.CommentsControllerTests.Get_Returns_Ok_With_CommentsList
       Source: CommentsControllerTests.cs line 22
       Duration: 128 ms

      Message: 
        Expected response to have a content equivalent to a model, but is has differences:

            - expected subjectModel to be a collection with 3 item(s), but {{ Author = Adrian, Content = Hey }, { Author = Johnny, Content = Hey! }}"
        "contains 1 item(s) less than"
        "{{ Author = Adrian, Content = Hey }, { Author = Johnny, Content = Hey! }, { Author = John, Content = Hey! }}.
        . 

        The HTTP response was:

        HTTP/1.1 200 OK
        Content-Type: application/json; charset=utf-8
        Content-Length: 102

        [
            {
                "author": "Adrian",
                "content": "Hey",
                "commentId": 1
            },
            {
                "author": "Johnny",
                "content": "Hey!",
                "commentId": 2
            }
        ]
        The originated HTTP request was:

        GET http://localhost/api/comments HTTP 1.1
        Cookie: 
        Content-Length: 0


      Stack Trace: 
        XUnit2TestFramework.Throw(String message)
        TestFrameworkProvider.Throw(String message)
        DefaultAssertionStrategy.HandleFailure(String message)
        AssertionScope.FailWith(Func`1 failReasonFunc)
        AssertionScope.FailWith(Func`1 failReasonFunc)
        AssertionScope.FailWith(String message, Object[] args)
        HttpResponseMessageAssertions.BeAs[TModel](TModel expectedModel, String because, Object[] becauseArgs) line 52
        CommentsControllerTests.Get_Returns_Ok_With_CommentsList() line 31
        --- End of stack trace from previous location where exception was thrown ---

This particular test uses Be200Ok and BeAs<> assertions. I will provide next the BeAs assertions to make a picture of how these are implemented

using FluentAssertions.Execution;
using FluentAssertions.Web.Internal;
using System;
using System.Net.Http;

namespace FluentAssertions.Web
{
    /// <summary>
    /// Contains a number of methods to assert that an <see cref="HttpResponseMessage"/> is in the expected state related to the HTTP content.
    /// </summary>
    public partial class HttpResponseMessageAssertions
    {
        /// <summary>
        /// Asserts that HTTP response content can be an equivalent representation of the expected model.
        /// </summary>
        /// <param name="expectedModel">
        /// The expected model.
        /// </param>
        /// <param name="because">
        /// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
        /// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
        /// </param>
        /// <param name="becauseArgs">
        /// Zero or more objects to format using the placeholders in <see paramref="because" />.
        /// </param>
        public AndConstraint<HttpResponseMessageAssertions> BeAs<TModel>(TModel expectedModel, string because = "", params object[] becauseArgs)
        {
            ExecuteSubjectNotNull(because, becauseArgs);

            if (expectedModel == null)
            {
                throw new ArgumentNullException(nameof(expectedModel), "Cannot verify having a content equivalent to a model against a <null> model.");
            }

            var success = TryGetSubjectModel<TModel>(out var subjectModel);

            Execute.Assertion
                     .BecauseOf(because, becauseArgs)
                     .ForCondition(success)
                     .FailWith("Expected {context:response} to have a content equivalent to a model, but the JSON representation could not be parsed{reason}. {0}",
                         Subject);

            string[] failures;

            using (var scope = new AssertionScope())
            {
                subjectModel.Should().BeEquivalentTo(expectedModel);

                failures = scope.Discard();
            }

            Execute.Assertion
                       .BecauseOf(because, becauseArgs)
                       .ForCondition(failures.Length == 0)
                       .FailWith("Expected {context:response} to have a content equivalent to a model, but is has differences:{0}{reason}. {1}",
                           new AssertionsFailures(failures),
                           Subject);

            return new AndConstraint<HttpResponseMessageAssertions>(this);
        }

        /// <summary>
        /// Asserts that HTTP response has content that matches a wildcard pattern.
        /// </summary>
        /// <param name="expectedWildcardText">
        /// The wildcard pattern with which the subject is matched, where * and ? have special meanings.
        /// </param>
        /// <param name="because">
        /// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
        /// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
        /// </param>
        /// <param name="becauseArgs">
        /// Zero or more objects to format using the placeholders in <see paramref="because" />.
        /// </param>
        public AndConstraint<HttpResponseMessageAssertions> MatchInContent(string expectedWildcardText, string because = "", params object[] becauseArgs)
        {
            Guard.ThrowIfArgumentIsNull(expectedWildcardText, nameof(expectedWildcardText), "Cannot verify a HTTP response content match a <null> wildcard pattern.");
            ExecuteSubjectNotNull(because, becauseArgs);

            var content = GetContent();

            if (string.IsNullOrEmpty(content))
            {
                Execute.Assertion
                    .BecauseOf(because, becauseArgs)
                    .FailWith("Expected {context:response} to match the wildcard pattern {0} in its content, but content was <null>{reason}. {1}",
                        expectedWildcardText,
                        Subject);
            }

            string[] failures;

            using (var scope = new AssertionScope())
            {
                content.Should().Match(expectedWildcardText);

                failures = scope.Discard();
            }

            Execute.Assertion
                       .BecauseOf(because, becauseArgs)
                       .ForCondition(failures.Length == 0)
                       .FailWith("Expected {context:response} to match a wildcard pattern in its content, but does not since:{0}{reason}. {1}",
                           new AssertionsFailures(failures),
                           Subject);

            return new AndConstraint<HttpResponseMessageAssertions>(this);
        }
    }
}

In order to tell FluentAssertions how to display failure messages, you can implement the IValueFormatter so I have one for HttpResponseMessage

using FluentAssertions.Formatting;
using FluentAssertions.Web.Internal;
using FluentAssertions.Web.Internal.ContentProcessors;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace FluentAssertions.Web
{
    internal class HttpResponseMessageFormatter : IValueFormatter
    {
        public bool CanHandle(object value) => value is HttpResponseMessage;

        /// <inheritdoc />
        public string Format(object value, FormattingContext context, FormatChild formatChild)
        {
            var response = (HttpResponseMessage)value;

            var messageBuilder = new StringBuilder();
            messageBuilder.AppendLine();
            messageBuilder.AppendLine();
            messageBuilder.AppendLine("The HTTP response was:");

            Func<Task> contentResolver = async () => await AppendHttpResponseMessage(messageBuilder, response);
            contentResolver.ExecuteInDefaultSynchronizationContext().GetAwaiter().GetResult();

            return messageBuilder.ToString();
        }

        private static async Task AppendHttpResponseMessage(StringBuilder messageBuilder, HttpResponseMessage response)
        {
            await AppendResponse(messageBuilder, response);
            await AppendRequest(messageBuilder, response);
        }

        private static async Task AppendResponse(StringBuilder messageBuilder, HttpResponseMessage response)
        {
            AppendProtocolAndStatusCode(messageBuilder, response);
            Appender.AppendHeaders(messageBuilder, response.GetHeaders());
            AppendContentLength(messageBuilder, response);
            await AppendResponseContent(messageBuilder, response);
        }

        private static async Task AppendRequest(StringBuilder messageBuilder, HttpResponseMessage response)
        {
            var request = response.RequestMessage;
            messageBuilder.AppendLine();
            if (request == null)
            {
                messageBuilder.AppendLine("The originated HTTP request was <null>.");
                return;
            }
            messageBuilder.AppendLine("The originated HTTP request was:");
            messageBuilder.AppendLine();

            messageBuilder.AppendLine($"{request.Method.ToString().ToUpper()} {request.RequestUri} HTTP {request.Version}");

            Appender.AppendHeaders(messageBuilder, request.GetHeaders());
            AppendContentLength(messageBuilder, request);

            messageBuilder.AppendLine();

            await AppendRequestContent(messageBuilder, request.Content);
        }

        private static async Task AppendResponseContent(StringBuilder messageBuilder, HttpResponseMessage response)
        {
            var content = response.Content;
            if (content == null)
            {
                return;
            }

            var processors = new List<IContentProcessor>();
            processors.Add(new InternalServerErrorProcessor(response, content));
            processors.AddRange(ProcessorsRunner.CommonProcessors(content));

            var contentBuilder = await ProcessorsRunner.RunProcessors(processors);
            messageBuilder.AppendLine();
            messageBuilder.Append(contentBuilder);
        }

        private static async Task AppendRequestContent(StringBuilder messageBuilder, HttpContent content)
        {
            await Appender.AppendContent(messageBuilder, content);
        }


        private static void AppendProtocolAndStatusCode(StringBuilder messageBuilder, HttpResponseMessage response)
        {
            messageBuilder.AppendLine();
            messageBuilder.AppendLine($@"HTTP/{response.Version} {(int)response.StatusCode} {response.StatusCode}");
        }

        private static void AppendContentLength(StringBuilder messageBuilder, HttpResponseMessage response)
        {
            if (!response.GetHeaders().Any(c => string.Equals(c.Key, "Content-Length", StringComparison.OrdinalIgnoreCase)))
            {
                messageBuilder.AppendLine($"Content-Length: {response.Content?.Headers.ContentLength ?? 0}");
            }
        }

        private static void AppendContentLength(StringBuilder messageBuilder, HttpRequestMessage request)
        {
            if (!request.GetHeaders()
                .Any(c => string.Equals(c.Key, "Content-Length", StringComparison.OrdinalIgnoreCase))
            )
            {
                request.Content.TryGetContentLength(out long contentLength);
                messageBuilder.AppendLine($"Content-Length: {contentLength}");
            }
        }
    }
}

This class and its collaborators have increased complexity and unfortunately, fewer unit tests. Thus this is also a source of pain points lately, so I will have at some moment to invest more in this regard. The collaborators are some classes that have the knowledge of building failure messages based on certain predicates, like the HttpContent type and/or other aspects of the subjected HttpResponse. So for example for a JSON it will print it and beautify it, for a binary type like data, just an informative error message and for the an internal server error it will try to parse the stack trace embedded in the developer page. Next is the JsonProcessor.

using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace FluentAssertions.Web.Internal.ContentProcessors
{
    internal class JsonProcessor : ProcessorBase
    {
        private readonly HttpContent _httpContent;
        public JsonProcessor(HttpContent httpContent)
        {
            _httpContent = httpContent;
        }

        protected override async Task Handle(StringBuilder contentBuilder)
        {
            var content = await _httpContent.SafeReadAsStringAsync();

            var beautified = content?.BeautifyJson();
            if (!string.IsNullOrEmpty(beautified))
            {
                contentBuilder.Append(beautified);
            }
        }

        protected override bool CanHandle()
        {
            if (_httpContent == null)
            {
                return false;
            }

            _httpContent.TryGetContentLength(out long length);
            var mediaType = _httpContent.Headers.ContentType?.MediaType;
            return length <= ContentFormatterOptions.MaximumReadableBytes
                   && (mediaType.EqualsCaseInsensitive("application/json") || mediaType.EqualsCaseInsensitive("application/problem+json"));
        }
    }
}

I feel at this moment that I’m close to a first production release, but I didn’t get yet through a code review, to see what others think about this tool, so I kindly ask you to help me out.

So there would be some of my questions:

  • How complete do you find the assertion API, does the current one fit
    the day-to-day work? Do you have more suggestions?
  • I focused on writing quality code and went also with some of the
    C# 8.0 features. I most likely missed some aspects, please point them
  • Do the samples fulfill their purpose?
  • Is the documentation relevant? Are the unit tests complete, beyond
    code coverage?
  • Any other aspects like CI/CD, automated builds, nuget releases?


Get this bounty!!!

#StackBounty: #c# #.net #events #visual-studio-2017 Can Visual Studio auto-create an inline, local anonymous method for an event instea…

Bounty: 50

When you use the += operator to create an event handler, Visual Studio gives you the option to press TAB to auto-create the handler. Doing so creates a concrete method like so:

consoleListBox.OnKeyPress += ConsoleListBox_OnKeyPress;

private static void ConsoleListBox_OnKeyPress(ConsoleControl sender, ConsoleKeyInfo keyInfo)
{

}

But is it possible to have VS create a local, inline anomyous handler like this?

consoleListBox.OnKeyPress += (sender, keyInfo) =>
{

};

It’s so much cleaner, especially if I don’t need the handler outside of the current context. I create these manually for now, but it would be nice if there were a way to auto-create local handlers.


Get this bounty!!!

#StackBounty: #.net #sql-server #sql-server-2017 #sqlpackage Can Sql Package Exe Run Post Deployment Script In Command Line?

Bounty: 50

The following will deploy Sql database with sqlpackage.exe.
This does not require use of Publish Profile Xml. Is there a way to add simple post-deployment script inline, without having external post-deployment file?

Eg: will publish database inline, without a publish profile xml.

SqlPackage.exe 
/Action:Publish 
/SourceFile:TestDatabase.dacpac
/TargetDatabaseName:TestDb
/TargetServerName:localhost

Now, goal to add post deployment script in command line, eg insert value in sample table.

Intended Goal:

SqlPackage.exe 
/Action:Publish 
/SourceFile:TestDatabase.dacpac
/TargetDatabaseName:TestDb
/TargetServerName:localhost
/PostDeploymentScript:"insert into dbo.SampleTable (SampleColumn) values ('1')"

Cannot find Post Deployment script inline option in Microsoft website. Maybe it doesn’t exist.

https://docs.microsoft.com/en-us/sql/tools/sqlpackage?view=sql-server-2017

SQLPackage Post Deployment Script not running


Get this bounty!!!

#StackBounty: #c# #.net #ml.net #ml.net-model-builder ML.Net Data.SaveAsText CSV file cannot be loaded by Model Builder

Bounty: 50

I export my current ML.Net Data to a CSV with this function

using (var stream = File.Create("c:\temp\aidata.csv"))
             mlContext.Data.SaveAsText(trainData, stream);          

The saved data looks ok, but when I try to load the CSV with the ML.Net Model Builder I get this error:

Data Error: Unable to infer column types of the file provided.

Note:
I also have vector columns in the CSV

The CSV file looks like this (I have removed some colums to be smaller).

If I load the data file with mlContext.Data.LoadFromTextFile(“c:tempaidata.csv”);
it loads it without any error, but the Model Builder fails to load it.


Get this bounty!!!

#StackBounty: #.net #oracle #entity-framework #ado.net Entity Framework 6 – StoreGeneratedPattern value not staying

Bounty: 50

We are using Entity Framework 6.2.0 against an Oracle 12c database, and have been following the Database-First approach.

A lot of out tables have an _ID column that is generated by the database upon INSERT. This is down by using a default value on the DB server, TABLE_SEQUENCE.NEXTVAL.

After generating the DB model in Entity Framework, I went through and marked each _ID column as StoreGeneratedPattern="Identity" and we were all set to go. However, when updating the DB model after some DB changes, we noticed an issue. One table in particular forgets this setting and resets this back to None. All other tables remember StoreGeneratedPattern="Identity".

This consistently happens each time we update the DB model from the database.
Visual Studio will still say that PROBLEMATIC_TABLE_ID has Identity set, but the .edmx file does not.
My process has been to update the model, then open the .edmx file and manually paste in StoreGeneratedPattern="Identity" for that one column.

I have compared this table against others in the DB, in the .edmx, in the codebase, and I cannot find anything that would cause this one column on this one table to behave differently upon DB model update.

TL;DR: Upon updating our local DB model from the database, the value for StoreGeneratedPattern on one column/table is always reset, while all others stay the same. VS still says it is Identity, but the XML in the .edmx file disagrees and needs to be updated manually.


Get this bounty!!!

#StackBounty: #c# #.net #exception #restsharp Exception not caught when thrown by RESTSharp

Bounty: 50

I’m using RestSharp to communicate with a REST-Server and wrote a small wrapper function for the call

private T Get<T>(string restAdress) where T : new()
{
    try
    {
        // throw new Exception(); // This one is caught
        IRestClient restClient = new RestClient(settings.Value.HostToConnect).UseSerializer(new JsonNetSerializer()); // (1)
        RestRequest restRequest = new RestRequest(restAdress);
        IRestResponse<T> result = restClient.Get<T>(restRequest); // (2)
        return result.Data;
    }
    catch (Exception e) // debugger wont stop here
    {
        // debugger wont stop here too
        // code within this block is not executed
    }

    return null; // (3)
}

Since I want to use the Newtonsoft-Attributes I give in a custom (de)serializer (1).

    public class JsonNetSerializer : IRestSerializer
    {
        public string Serialize(object obj) =>
            JsonConvert.SerializeObject(obj);

        public string Serialize(RestSharp.Parameter bodyParameter) =>
            JsonConvert.SerializeObject(bodyParameter.Value);

        public T Deserialize<T>(IRestResponse response) =>
            JsonConvert.DeserializeObject<T>(response.Content); // (4)

        public string[] SupportedContentTypes { get; } =
        {
            "application/json", "text/json", "text/x-json", "text/javascript", "*+json"
        };

        public string ContentType { get; set; } = "application/json";

        public DataFormat DataFormat { get; } = DataFormat.Json;
    }

When calling the REST service and trying to get the result (2) an Exception is thrown if the deserialization fails (4). But the Exception is not caught by the try-catch block. I tried to debug but after throwing the debugger goes on in line (3), the catch and the logging withing the catch is never executed. The debugger won’t even stop at the catch (Exception e) it goes straight from (4) to (3).

enter image description here
(Sorry for not english, the title of the window says “Exception User-Unhandled”)

Can anyone explain this behaviour to me?


Get this bounty!!!